Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Foreign keys
Multicolumn foreign keys
Updates and deletes
Revision
26. Revision - #3

Instruction

That's 100% correct! Are you ready for the final challenge?

Exercise

We have two tables: owner and pet. Each pet belongs to an owner. We want you to create the table pet with the following columns:

  • id - int, primary key,
  • name - up to 32 characters,
  • type - up to 32 characters,
  • owner_id - an int. It is a foreign key referring to column id in the table owner. This time, however, we want to change the default behavior: we want the database to automatically make the necessary modifications when the foreign key is updated/deleted.

Stuck? Here's a hint!

Type:

CREATE TABLE pet (
  id int PRIMARY KEY,
  name varchar(32),
  type varchar(32),
  owner_id int,
  FOREIGN KEY (owner_id) REFERENCES owner(id) ON DELETE CASCADE ON UPDATE CASCADE
);