Table Design Learning 1: Primary Key _ MySQL

Source: Internet
Author: User
Table Design Learning 1: Primary key bitsCN.com

1. the data in the table should be atomic, that is, the information contained in the column can satisfy the most efficient query operations, but this is still not enough to reach the first paradigm.

2. each data row must have a unique identifier, namely, the so-called Primary Key)

3. the primary key is a column in the table, which makes each record unique.

4. duplicate data columns cannot be used as primary keys, and ID IDs are not supported. Once a database is intruded, customer data is exposed.

5. the primary key cannot be NULL. if the primary key is NULL, it cannot be unique because other records may contain NULL.

6. when inserting a new record, you must specify the primary key value. if there is no primary key value, it means there is a risk that the primary key value is NULL, and duplicate records may exist in the table. this violates the standard principle.

7. the primary key must be clean and concise, and there should be no redundant content

8. the primary key value cannot be modified. if the primary key value can be changed, the uniqueness of the primary key may be damaged by accidental input of used values.

9. the best way to create a primary key is to create a column that contains the unique primary key, such as the ID column.

Example:

Mysql> create table student (-> id int primary key not null,-> first_name VARCHAR (20),-> last_name VARCHAR (30)->); Query OK, 0 rows affected( 0.16 sec)

Set the id column as the primary key and set the not null constraint so that it is NOT empty.

Create two atomic data columns respectively.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.