Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CASCADE and RESTRICT? SQL DDL database

Tags:

sql

database

ddl

Could anyone tell me what exactly CASCADE, and RESTRICT mean? It's in database systems subject to the DDL Part

And what if I don't write any of them in my ON DELETE statement?

like image 297
user3102872 Avatar asked Dec 16 '13 19:12

user3102872


People also ask

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 Cascade and restrict in drop table SQL?

CASCADE has the effect of dropping all SQL objects that are dependent on that object. RESTRICT is the default for the drop behavior. RESTRICT looks to see what objects are dependent on the object being dropped. If there are dependent objects, then the dropping of the object does not occur.

What is Cascade in SQL?

Cascade in SQL is used to delete or update an entry from both the child and the parent table simultaneously. The keyword CASCADE is used as conjunction while writing the query of ON DELETE or ON UPDATE.

What is restrict in SQL?

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 .


2 Answers

The ON DELETE CASCADE and ON DELETE RESTRICT are the foreign key properties and you set them when you create the relationship between two tables.

If you set the relationship to be ON DELETE CASCADE, when you run a DELETE statement on a "parent" table, it will automatically DELETE all the corresponding rows from the "child" table. But the RESTRICT (which is the default foreign key relationship behavior) is when you try to delete a row from the "parent" table and there is a row in the "child" table with the same ID, it will fail, complaining about the existing child rows.

Either way, you don't need to mention anything in your DELETE clause.

I also wrote a blog post about the different rules for DELETE and UPDATE commands in more detail here:

SQL Server Foreign Key Update and Delete Rules | Koukia

like image 166
Aram Avatar answered Oct 11 '22 21:10

Aram


There are three types of on delete associated with foreign key

  1. On Delete Cascade: when data is removed from a parent table, automatically data deleted from child table (foreign key table).
  2. On Delete set Null: when data is removed from a parent table, the foreign key associated cell will be null in a child table.
  3. On Delete Restrict: when data is removed from a parent table, and there is a foreign key associated with child table it gives error, you can not delete the record.
like image 32
Aditya Parmar Avatar answered Oct 11 '22 20:10

Aditya Parmar