mysql-Constraint type

Source: Internet
Author: User


MySQL Constraint type:

Primary KEY constraints, unique key constraints, foreign key constraints, non-null constraints, table properties Auto-increment, set Default values for field fields


1. PRIMARY KEY constraint (keyword: PRIMARY key):

       The    primary key is a special field of the table. This field uniquely identifies each piece of information in the table. The relationship between a primary key and a record is like a relationship between a person and a man. The primary key is used to identify each record, and the primary key value for each record is different. is used to indicate a person's identity, everyone has a unique * * * number. Setting a table's primary key is designed to set a table when a table is a field primary key

       The primary purpose of the    primary key is to help MySQL find a piece of information in the table at the fastest speed. The primary key must meet the condition that primary key value must be unique , the value of the primary key field for any two records in the table cannot be the same; < Span style= "font-family: ' Microsoft Jas Black ', ' Microsoft Yahei '; font-size:20px;" The value of the primary key is a non-null value of . The primary key can be single field , can also be A combination of multiple fields .

Simply put: if a field is defined as a primary key, the field value must be unique and cannot be empty. Each table can define only one primary key, but you can define multiple fields to be combined into one primary key type .


2. Unique key constraint (keyword: unique):

Uniqueness means that the value of the field in all records cannot recur. Setting a table's uniqueness constraint is when you create a table, some special fields of the enclosing tag are added with a unique constraint. A uniqueness constraint will guarantee that the value is unique in the unique field.

In simple terms: If a field is defined as a unique key, the field value must be unique and can be null. Each table can define multiple unique keys.


3, FOREIGN KEY constraints (keywords: CONSTRAINT, FOREIGN Key, REFERENCES):

A foreign key is a special field of a table. The main is to establish the association between tables. The foreign key must depend on the primary key of the parent table that the database already exists.

Simply put: if a field is defined as a foreign key, the field needs to rely on the primary key of another table, which is associated with another table. The other table is the parent table of this table, and the foreign key can be null. And the child table foreign key data type must be the same as the parent table data type.


4, non-null constraint (keyword: NOT null):

Ensure that the value inserted by this field cannot be empty.


5. Automatic increment of table attributes (Auto_increment):

Field values are automatically incremented, no duplicate values occur, and are generally not empty.


6. Set the default value of the table field (keyword: default):

You can specify the default values for the fields in the table when you create the table. If you do not assign a value to this field when inserting a new record, the database system automatically inserts a default value for that field.

mysql-Constraint type

Related Article

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.