Objective
Learn MySQL Today
The primary key, also called the main code, is a combination of one or more columns in a table. A PRIMARY key constraint requires that the primary key's data is unique and is not allowed to be empty. A primary key uniquely identifies a record in a table, can be combined with a foreign key to define relationships between different data tables, and can speed up database queries. The relationship between the primary key and the record is like the identity card and the human relationship, they are one by one corresponding, the primary key is divided into two types: single Chini and multi-field federated primary key.
1. Single Field primary key
The single-field primary key consists of one, and the SQL statement format is divided into the following two scenarios.
(1) Specify the primary key while defining the column, with the following syntax rules:
Field name data type PRIMARY KEY [default value]
650) this.width=650; "width=" 499 "height=" 225 "title=" 2017-10-14_102532.png "style=" WIDTH:277PX;HEIGHT:137PX; "src=" Https://s5.51cto.com/wyfs02/M01/A7/21/wKioL1nhdhzQbGRLAAAKyTS0w0I751.png-wh_500x0-wm_3-wmp_4-s_625504916.png " alt= "Wkiol1nhdhzqbgrlaaakyts0w0i751.png-wh_50"/>
2) in the Definition data table TB_EMP2, its primary key is the ID,SQL statement as follows:
[constraint< constraint name;] PRIMARY key [field name]
Example: Define the data table Tb_emp3, whose primary key is the ID,SQL statement as follows:
650) this.width=650; "title=" 2017-10-14_105327.png "src=" https://s5.51cto.com/wyfs02/M00/08/6B/ Wkiom1nhfbhtbuvhaaajlzdfy4c085.png "alt=" Wkiom1nhfbhtbuvhaaajlzdfy4c085.png "/>
Using Uniqueness constraints
The unique constraint requires that the column be unique, allowed to be empty, but only one null value can appear. A unique constraint ensures that a column or columns do not have duplicate values.
The syntax rules for uniqueness constraints are as follows:
(1) A constraint is specified directly after the column is defined, and the grammar rules are entered.
Field name data type unique
Example: Define the data table TB_DEPTL, specify the name of the department unique, the SQL statement is as follows:
650) this.width=650; "title=" 2017-10-14_110031.png "src=" https://s1.51cto.com/wyfs02/M00/A7/21/ Wkiol1nhewyqpbpraaakkwkegea462.png "alt=" Wkiol1nhewyqpbpraaakkwkegea462.png "/>
Here we go!
This article from "Li Shilong" blog, declined reprint!
MySQL 5.7 using PRIMARY KEY constraints