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

  1. Two rows in a table have the same primary key value. Is that allowed? (a) yes, (b) no.
  2. Can a primary key column contain NULL? (a) yes, (b) no.
  3. Which is usually the safest primary key for a users table? (a) the user's name, (b) an id number the database creates, (c) the user's age.
  4. 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.
  5. orders.customer_id points to customers.id. This is a: (a) one-to-many relationship, (b) many-to-many relationship, (c) no relationship.
  6. 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.
  7. 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.
  8. A primary key made of two columns together is called a: (a) composite key, (b) broken key, (c) foreign key.
  9. Should you change a row's primary key value often? (a) yes, (b) no, ideally never.
  10. 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

  1. (b) no. Uniqueness is the whole point of a primary key.
  2. (b) no. A primary key must always have a value, so every row can be found.
  3. (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.
  4. (b). It "points" to a row in another table. It is how an order knows which customer it belongs to.
  5. (a) one-to-many. One customer can have many orders; each order has one customer.
  6. (b) a linking table, often called a junction table, with a student_id and a course_id in each row.
  7. (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;.)
  8. (a) composite key. A junction table often uses (student_id, course_id) as its primary key, so the same pair cannot appear twice.
  9. (b) no. Other tables point to it. Changing it means changing every reference. That is another reason ids are popular.
  10. (a) the ON DELETE rule. Common choices are to refuse the delete, to delete the orders too (CASCADE), or to set their customer_id to NULL. Choose carefully; CASCADE can remove more than you expect.

Scoring

ScoreSuggestion
0 to 4Course Outline: Understanding Primary Keys From Scratch
5 to 7How to Add a Foreign Key Between Two Tables
8 to 10One-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.

Related pages on this site