RESTRICT, NO ACTION, CASCADE, SET NULL in Mysql foreign key settings

RESTRICT, NO ACTION, CASCADE, SET NULL in Mysql foreign key settings


When I looked at the company's permission table structure today, I found that there is no association between the tables, and all the associations are implemented in the backend. This always feels a bit wrong, so use Navicat for mysql to design the table structure.

Before this, we must first understand the concept of parent table and child table. Simply put, when two tables are connected by a foreign key, in which table the foreign key is the primary key, that table is the parent table, and the other table is the child table.

When setting a foreign key, there are four values ​​to choose from when deleting and updating two columns: RESTRICT, NO ACTION, CASCADE, SET NULL, their functions are as follows:

  • CASCADE : When the parent table is deleted and updated, the child table will delete and update the associated records;
  • SET NULL : When the parent table is deleted and updated, the child table will set the column of the foreign key field of the associated record to null, so note that the foreign key cannot be set to not null when designing the child table;
  • RESTRICT : If you want to delete a record in the parent table, and there are records in the child table that are associated with the parent table, you are not allowed to delete the records in the parent table;
  • NO ACTION : Same as RESTRICT, but first check the foreign key;

Guess you like

Origin blog.csdn.net/weixin_42201180/article/details/107400226