MySQL Lesson three

Source: Internet
Author: User

MySQL FOREIGN key

1. What is a foreign key

A specific relationship between a table and a table. Maintain the integrity and consistency of your data.

User table

Order Orders Form

1.1 Delete the user, do not delete the order, the data is inconsistent

Insert record in 1.2 order table

Foreign Key control: Let the user do not update the data, or the user delete the data when the order synchronization also deleted

2. Foreign Key Features:

2.1 To see if a foreign key exists:

2.1.1 InnoDB type

Show CREATE TABLE name:

engine--Storage Engine

The 2.1.2 foreign key is a constraint relationship between two tables.

2.1.3 The name of the foreign key is unique.

3. Create key FOREIGN key

CREATE TABLE table name (column definition index definition foreign key definition [constraint constraint name] foreign key[foreign key field]

references[foreign Key Name] (Foreign key field)

[on Delete{retrict|cascade|set null | no action}]

[on Update{restrict|cascade|set null|no Action}])

Restrict: Deny delete or update to parent table

Cascade: Delete or update of the parent table, automatically delete or update the corresponding record in the child table.

Set NULL parent table Delete, UPDATE, setting Word table foreign key field null

No action: Do not act.

To delete a foreign key:

MySQL Lesson three

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.