I'm trying to make a FK on a column, and now I'm thinking when exactly should I use ON DELETE RESTRICT
? (or ON UPDATE RESTRICT
). Isn't it the same as NO ACTION
?
Well ON DELETE RESTRICT
means you can't delete a given parent row if a child row exists that references the value for that parent row. If the parent row has no referencing child rows, then you can delete that parent row. Well its definition is the default behavior for a foreign key anyway.
Am I missing something?
They're equivalent. It even says so right there in the documentation:
NO ACTION
: A keyword from standard SQL. In MySQL, equivalent toRESTRICT
.
There's a difference between them in databases that have deferred checks, but MySQL doesn't.
The only difference is coming out when you define a constraint as deferrable with an initially deferred or initially immediate mode.
NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not allowed to proceed if there is a related foreign key value in the referenced table. InnoDB rejects the delete or update operation for the parent table.
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. (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.)
See: SQL SET CONTRAINS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With