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!
Deze oefening maakt deel uit van de cursus
Introduction to Relational Databases in SQL
Oefeninstructies
- 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)!
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
-- Try to insert a new professor
INSERT INTO professors (firstname, lastname, university_id)
VALUES ('Albert', 'Einstein', 'MIT');