SQLQuestion 05 of 14
How many primary keys can a table have?
Answer out loud or in your head first, then compare.
Answer
A table has at most one PRIMARY KEY, but it can consist of several columns. It can, however, have many UNIQUE constraints.
SQL
CREATE TABLE memberships (
user_id bigint,
team_id bigint,
PRIMARY KEY (user_id, team_id)
);To be precise: the primary key enforces uniqueness of the combination and no NULLs in its columns. In the example a user can belong to many teams, but only once to a given team.
Pitfall: two columns in a composite key don't mean two primary keys. A foreign key is a different constraint — it makes sure references point at existing data.
How did it go? Be honest — “Not yet” puts the question on your review list.