Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Primary keys
Foreign keys
Updating and deleting with foreign keys
Summary

Instruction

Just as we expected – we failed. Why is that so? Because when we write REFERENCES... after FOREIGN KEY (...):

FOREIGN KEY (...) REFERENCES ...
we actually mean:
REFERENCES ... ON UPDATE RESTRICT ON DELETE RESTRICT
In other words, the database won't allow us to update or delete a primary key that's linked to another table via a foreign key.

There are, however, other options we can use instead of RESTRICT:

  1. CASCADE – When the referenced row is deleted or updated, the respective rows of the referencing table will be deleted or updated.
  2. NO ACTION – Means "Do nothing to the referenced row". (In our database, this is the same as RESTRICT; in some databases, NO ACTION is slightly different than RESTRICT).
  3. SET NULL – The values of the affected rows are set to NULL.
  4. SET DEFAULT – The values of the affected rows are set to their default values.

Exercise

Run the template code, which creates a new version of the project table – this time with CASCADE. Take a close look at how it's done.