SQLBeginner#keys

Primary key vs unique key vs foreign key?

A primary key uniquely identifies a row and is NOT NULL (one per table). A unique key also enforces uniqueness but allows a NULL and can appear many times. A foreign key references another table's key and enforces referential integrity.

Example
CREATE TABLE users (
  id INT PRIMARY KEY,
  email VARCHAR(100) UNIQUE,
  dept_id INT REFERENCES departments(id)
);

Related Questions

1
SQLBeginner#keys

What is a composite key?

Open
2
SQLIntermediate#keys

What is a candidate key and a super key?

Open
3
SQLIntermediate#normalization

What is normalization? Explain 1NF, 2NF and 3NF.

Open