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!
This exercise is part of the course
Introduction to Relational Databases in SQL
Exercise instructions
- Run the sample code and have a look at the error message.
- What's wrong? Correct the
university_id
so that it actually reflects where Albert Einstein wrote his dissertation and became a professor – at the University of Zurich (UZH)!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Try to insert a new professor
INSERT INTO professors (firstname, lastname, university_id)
VALUES ('Albert', 'Einstein', 'MIT');