... user_id INTEGER NOT NULL, CONSTRAINT fk_user_meta FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT
I know from here that ON DELETE CASCADE means that if I delete a row from the users table, then the associated row from the user meta table will be removed too. But what does ON UPDATE RESTRICT do?
When do we use ON DELETE RESTRICT? Whenever we don't want "orphan" rows in the database! We don't want to delete a customer from the CUSTOMER table if there are any orders for that customer in the ORDERS table.
CASCADE. It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is either deleted or updated when the parent data is deleted or updated.
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.
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 .
RESTRICT
prevents the action from happening if there's any foreign keys that rely on the field that's being changed.
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