Use of MySQL foreign key constraints OnDelete and OnUpdate _ MySQL

Source: Internet
Author: User
Both OnDelete and OnUpdate have the Restrict, NoAction, Cascade, and SetNull attributes. Now let's explain the meaning of their properties separately. ONDELETErestrict: when deleting the corresponding record in the parent table (that is, the source table of the foreign key), first check that the record is On Delete and On Update both have Restrict, No Action, cascade, Set Null attribute. Now let's explain the meaning of their properties separately.

ON DELETE

Restrict): When deleting a corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has a corresponding foreign key. If yes, deletion is not allowed.

No action:The same as restrict. that is, if the slave data exists, the master data cannot be deleted.

Cascade ):When deleting a corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has a corresponding foreign key, if any, the records of foreign keys in the sub-table (that is, the table containing foreign keys) are also deleted.

Set null:When deleting a corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has a corresponding foreign key, if yes, set the foreign key value in the subtable to null (however, this requires that the foreign key be allowed to be null)

ON UPDATE

Restrict): When updating the corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has the corresponding foreign key. If yes, update is not allowed.

No action:The same as restrict.

Cascade ):When updating the corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has the corresponding foreign key, if any, the record of the foreign key in the sub-table (that is, the table containing the foreign key) is also updated.

Set null:When updating the corresponding record in the parent table (that is, the source table of the foreign key), first check whether the record has the corresponding foreign key, if yes, set the foreign key value in the subtable to null (however, this requires that the foreign key be allowed to be null ).

Note: the difference between "no action" and "RESTRICT" is that the operation is executed after the ACTION with other constraints, and that of "no action" has the highest priority.

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.