SQL foreign key constraints

Source: Internet
Author: User

No action
If you try to delete or modify a row, and the row's key is referenced by the foreign key in the existing row of another table, an error is generated and the delete/update statement is rolled back.
Cascade, set null, and set default
Tables with foreign key relationships can be affected by deleting or updating key values. These foreign key relationships can be traced back to the tables in which they are modified. If cascade reference is also defined for the target table, the specified cascade operation will also be applied to the rows to be deleted or updated. Cascade cannot be specified for a foreign key or primary key with a timestamp column.
On Delete Cascade
Specify that if you try to delete a row, and the row's key is referenced by the foreign key in the existing row of other tables, all rows containing those foreign keys will also be deleted.
On update Cascade
If you try to update the key value of a row, and the key value of this row is referenced by the foreign key in the existing row of another table, all values that constitute the foreign key will also be updated to the new value specified for the key. (If the timestamp column is a foreign key or a part of the referenced key, cascade cannot be specified. )
On Delete set null
Specify that if you try to delete a row, and the row's key is referenced by the foreign key in the existing row of another table, all values that constitute the foreign key in the referenced row will be set to null. All foreign key columns in the target table must be null. This constraint can only be executed.
On update set null
Specify that if you try to update a row, and the row's key is referenced by the foreign key in the existing row of another table, all values that constitute the foreign key in the referenced row will be set to null. All foreign key columns in the target table must be null. This constraint can only be executed.
On Delete set default
Specify that if you try to delete a row, and the key of this row is referenced by the foreign key in the existing row of other tables, all values that constitute the Foreign keys in the referenced row are set as their default values. All foreign key columns in the target table must have a default value. This constraint can be executed. If a column can be null without an explicit default value, null is used as the implicit default value for the column. Any non-null value set due to on Delete set default must have a corresponding value in the master table to maintain the validity of the foreign key constraint.
On update set default
If you try to update a row, and the key of the row is referenced by the foreign key in the existing row of other tables, all values that constitute the Foreign keys in the referenced row are set as their default values. All foreign key columns in the target table must have a default value. This constraint can be executed. If a column can be null without an explicit default value, null is used as the implicit default value for the column. Any non-null value set due to on update set default must have a corresponding value in the master table to maintain the validity of the foreign key constraint.

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.