MySQL Learning (MU lesson Note 5) constraints

Source: Internet
Author: User

Constraint type:

1, NOT NULL (non-null constraint)

2. PRIMARY key (primary KEY constraint)

Only one primary key can exist per data table

The primary key guarantees the uniqueness of the record

Primary key is not NULL automatically

(Auto_increment automatic coding)

3. Unique KEY (single constraint)

Unique constraints guarantee the uniqueness of records

A field with a unique constraint can be a null value

Multiple unique constraints can exist for each data table

When you insert the same content into a username field with a unique constraint, the system prompts an error

Default constraint

Add a DEFAULT constraint to the Sex field

When the sex field is empty, the system records it as 3 by default

FOREIGN key (FOREIGN KEY constraint)

Maintain data consistency, integrity

Implementing one-to-one or one-to-many relationships

MySQL configuration file

Default_storage-engine=innodb

Create a parent table

stored in InnoDB

SHOW CREATE TABLE Province;

Create child table

FOREIGN KEY (PID) REFERENCES prvince (ID)

Parent table Reference column does not have an index created

If no index exists for the foreign key, MySQL will automatically create

Referential actions for FOREIGN KEY constraints

Cascade Delete or update from the parent table and automatically delete or update matching rows in the child table

Set NULL deletes or updates rows from the parent table and sets the foreign key column in the child table to null

Restrict deny Delete or update to parent table

No action standard SQL keyword, same as restrict in MySQL

To add a parent table to see the changes in the child table

Delete records from id=3 in Main Table province

The record of PID 3 in child table User1 is also deleted

A constraint established on a data column, called a column-level constraint

Constraints that are established on multiple data columns, called Table-level constraints

MySQL Learning (MU lesson Note 5) 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.