Explore foreign key constraints
Foreign key constraints help you to keep order in your database mini-world. In your database, for instance, only professors belonging to Swiss universities should be allowed, as only Swiss universities are part of the universities table.
The foreign key on professors referencing universities you just created thus makes sure that only existing universities can be specified when inserting new data. Let's test this!
Latihan ini adalah bagian dari kursus
Introduction to Relational Databases in SQL
Petunjuk latihan
- Run the sample code and have a look at the error message.
- What's wrong? Correct the
university_idso that it actually reflects where Albert Einstein wrote his dissertation and became a professor – at the University of Zurich (UZH)!
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
-- Try to insert a new professor
INSERT INTO professors (firstname, lastname, university_id)
VALUES ('Albert', 'Einstein', 'MIT');