Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the different between RESTRICT and NO ACTION?

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?

like image 214
Shafizadeh Avatar asked Sep 05 '16 22:09

Shafizadeh


2 Answers

They're equivalent. It even says so right there in the documentation:

NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT.

There's a difference between them in databases that have deferred checks, but MySQL doesn't.

like image 138
Barmar Avatar answered Oct 23 '22 08:10

Barmar


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

like image 21
Vural Avatar answered Oct 23 '22 07:10

Vural