Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql workbench foreign key options [Restrict, Cascade, Set Null, No Action], what do they do?

In foreign key options on update and on delete. What does each field [Restrict, Cascade, Set Null, No Action] do?

like image 767
user2310173 Avatar asked Apr 23 '13 07:04

user2310173


People also ask

When using a foreign key what are the options Cascade and set null used for?

Between two tables, do not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table. SET NULL: Delete or update the row from the parent table, and set the foreign key column or columns in the child table to NULL.

What is no action in foreign key?

NO ACTION : A keyword from standard SQL. In MySQL, equivalent to RESTRICT . The MySQL Server rejects the delete or update operation for the parent table if there is a related foreign key value in the referenced table. Some database systems have deferred checks, and NO ACTION is a deferred check.

What is difference between restrict and Cascade?

The CASCADE option directs the DBMS Server to revoke the specified privileges plus all privileges and objects that depend on the privileges being revoked. The RESTRICT option directs the DBMS Server not to revoke the specified privilege if there are any dependent privileges or objects.

What is the difference between Cascade delete and set null on delete?

ON DELETE CASCADE : SQL Server deletes the rows in the child table that is corresponding to the row deleted from the parent table. ON DELETE SET NULL : SQL Server sets the rows in the child table to NULL if the corresponding rows in the parent table are deleted.


3 Answers

If you take one by one :

For both update and delete :

if you try to update / delete the parent row :

Restrict : Nothing gonna be delete if there is a child row

Cascade : the child row will be delete / update too

Set Null : the child column will be set to null if you delete the parent

No action : The child row will not be concern of the delete / update

like image 93
Axel Agarrat Avatar answered Oct 09 '22 18:10

Axel Agarrat


The table containing the foreign key is called the referencing or child table, and the table containing the candidate key is called the referenced or parent table.

Set NULL : Sets the column value to NULL when you delete the parent table row.

CASCADE : CASCADE will propagate the change when the parent changes. If you delete a row, rows in constrained tables that reference that row will also be deleted, etc.

RESTRICT : RESTRICT causes you can not delete a given parent row if a child row exists that references the value for that parent row.

NO ACTION : NO ACTION and RESTRICT are very much alike. when an UPDATE or DELETE statement is executed on the referenced table, the DBMS verifies at the end of the statement execution that none of the referential relationships are violated. in short child row no concern if parent row delete or update.

like image 35
Jaykumar Patel Avatar answered Oct 09 '22 17:10

Jaykumar Patel


When an UPDATE or DELETE operation affects a key value in the parent table that has matching rows in the child table, the result depends on the referential action specified using ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause. MySQL supports five options regarding the action to be taken, listed here:

CASCADE: Delete or update the row from the parent table, and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, do not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.

SET NULL: Delete or update the row from the parent table, and set the foreign key column or columns in the child table to NULL. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.

If you specify a SET NULL action, make sure that you have not declared the columns in the child table as NOT NULL.

RESTRICT: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause.

NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT. The MySQL Server rejects the delete or update operation for the parent table if there is a related foreign key value in the referenced table. Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT.

SET DEFAULT: This action is recognized by the MySQL parser, but InnoDB rejects table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.

For an ON DELETE or ON UPDATE that is not specified, the default action is always RESTRICT.

Copied above text from: https://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html

like image 1
Hemanth Avatar answered Oct 09 '22 17:10

Hemanth