MySQL foreign KEY constraints OnDelete and OnUpdate use

Source: Internet
Author: User

Both on delete and on update have restrict,no Action, cascade,set null property. Now explain the meaning of their attributes separately.

On DELETE

Restrict (constraint): When the corresponding record is deleted in the parent table (that is, the source table of the foreign key), first check that the record has a corresponding foreign key, and if there is one, it is not allowed to delete.

No action: The meaning is the same as restrict. That is, if there is data from, the deletion of master data is not allowed.

Cascade (cascading): when the corresponding record is deleted in the parent table (that is, the source table of the foreign key), first check that the record has a corresponding foreign key, and if there is one, delete the record in the child table (that is, the table that contains the foreign key).

set NULL: when the corresponding record is deleted in the parent table (that is, the source table of the foreign key), first check that the record has a corresponding foreign key, and if so, set the foreign key value in the child table to null (but this requires that the foreign key is allowed to take null)

On UPDATE

Restrict (constraint): When the corresponding record is updated in the parent table (that is, the source table of the foreign key), first checks whether the record has a corresponding foreign key and, if there is one, does not allow the update.

no action: mean with restrict.

Cascade (cascading): when the corresponding record is updated in the parent table (that is, the source table of the foreign key), first checks whether the record has a corresponding foreign key, and if so, updates the record in the child table (that is, the table that contains the foreign key).

set NULL: when the corresponding record is updated in the parent table (that is, the source table of the foreign key), first check that the record has a corresponding foreign key and, if there is one, set the foreign key value in the child table to null (this requires that the foreign key is allowed to take null).

Note: The difference between NO action and restrict: Only in the case of individual cases, the former is executed after the action of the other constraint, which has the highest priority execution.

MySQL foreign KEY constraints OnDelete and OnUpdate use

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.