Ten Questions on Primary and Foreign Keys
Keys are how a database tells rows apart and links tables together. This quiz checks the essentials. Read each question, choose an answer, then compare with the explanations below.
Questions
- Two rows in a table have the same primary key value. Is that allowed? (a) yes, (b) no.
- Can a primary key column contain NULL? (a) yes, (b) no.
- Which is usually the safest primary key for a
userstable? (a) the user's name, (b) an id number the database creates, (c) the user's age. - A foreign key is: (a) a key from another country, (b) a column that refers to the primary key of another table, (c) a spare password.
orders.customer_idpoints tocustomers.id. This is a: (a) one-to-many relationship, (b) many-to-many relationship, (c) no relationship.- Students can take many courses, and courses have many students. What do you usually need? (a) nothing extra, (b) a third table that links them, (c) a very long text column.
- You try to insert an order with
customer_id = 99, but there is no customer 99. With a foreign key in place, the database will: (a) accept it, (b) refuse it. - A primary key made of two columns together is called a: (a) composite key, (b) broken key, (c) foreign key.
- Should you change a row's primary key value often? (a) yes, (b) no, ideally never.
- If you delete a customer who still has orders, what happens depends on: (a) the foreign key's ON DELETE rule, (b) the weather, (c) nothing, it always works.
Answers explained
- (b) no. Uniqueness is the whole point of a primary key.
- (b) no. A primary key must always have a value, so every row can be found.
- (b) a database-created id. Names repeat and change; ages change every year. An id that means nothing outside the database never needs to change.
- (b). It "points" to a row in another table. It is how an order knows which customer it belongs to.
- (a) one-to-many. One customer can have many orders; each order has one customer.
- (b) a linking table, often called a junction table, with a
student_idand acourse_idin each row. - (b) refuse it. This protection is called referential integrity. It stops "orphan" rows that point to nothing. (In SQLite, foreign key checks must be switched on with
PRAGMA foreign_keys = ON;.) - (a) composite key. A junction table often uses
(student_id, course_id)as its primary key, so the same pair cannot appear twice. - (b) no. Other tables point to it. Changing it means changing every reference. That is another reason ids are popular.
- (a) the ON DELETE rule. Common choices are to refuse the delete, to delete the orders too (CASCADE), or to set their
customer_idto NULL. Choose carefully; CASCADE can remove more than you expect.
Scoring
| Score | Suggestion |
|---|---|
| 0 to 4 | Course Outline: Understanding Primary Keys From Scratch |
| 5 to 7 | How to Add a Foreign Key Between Two Tables |
| 8 to 10 | One-to-Many and Many-to-Many, Drawn Out |
Try it
Create two tiny tables, authors and books, with a foreign key from books to authors. Then try to insert a book with an author id that does not exist. Seeing the database say "no" is the best way to remember question 7.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.