Super, candidate, primary, alternate, and foreign keys — how a database tells rows apart and links tables together.
Keys are how a database tells rows apart and links tables together. Any set of columns whose value is different for every row — so it can never confuse two rows — is a super key. Trim a super key until nothing is wasted and you have a candidate key. Pick one candidate to be the official identifier and it becomes the primary key; the leftovers are alternate keys.
UNIQUE rule.Student(roll_no, email, name, dept_id). Both roll_no and email are unique, so each is a candidate key. {roll_no, name} is a super key (unique, but name is dead weight). Choose roll_no as the primary key → email becomes the alternate key.CREATE TABLE Student (
roll_no INT PRIMARY KEY, -- chosen candidate = primary key
email VARCHAR(80) UNIQUE, -- other candidate = alternate key
name VARCHAR(60),
dept_id INT REFERENCES Department(dept_id) -- foreign key
);The keys above all *identify* their own rows. A foreign key does the opposite job: it doesn't identify anything, it points at another table's primary key to link the two tables and keep them consistent. Above, dept_id in Student points at Department(dept_id) — every student's department must be a real one.
NULL means 'no value'. A primary key must be unique *and* not NULL, because a row with no identifier could never be found or referenced. A foreign key, however, may sometimes be NULL — meaning 'not linked yet'.