MySQL 5.7 using PRIMARY KEY constraints

Source: Internet
Author: User

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

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.