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?
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.
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.
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.
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 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
There are three types of on delete associated with foreign key
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