TExcelTAD
Home › SQL › SQL PRIMARY KEY

SQL PRIMARY KEY

The primary key uniquely identifies each row in a table. A table has at most one primary key, and its values must be unique and not NULL. Other tables refer to rows through it (foreign keys).

Every TAD Market table has a single-column primary key such as customer_id or order_id.

Defining a primary key

sql · try it yourself
CREATE TABLE warehouses (
  warehouse_id INTEGER PRIMARY KEY,
  city         TEXT NOT NULL
);

INSERT INTO warehouses (warehouse_id, city) VALUES (1, 'Rotterdam'), (2, 'Leipzig');
SELECT * FROM warehouses ORDER BY warehouse_id;

INSERT INTO warehouses (warehouse_id, city) VALUES (2, 'Hamburg');
2 rows affected.

warehouse_id | city
-------------+----------
1            | Rotterdam
2            | Leipzig
(2 rows)

Error: UNIQUE constraint failed: warehouses.warehouse_id

INTEGER PRIMARY KEY is special in SQLite

In SQLite, a column declared exactly as INTEGER PRIMARY KEY becomes an alias for the table's internal rowid. If you don't supply a value, SQLite picks one — the largest existing id plus one:

sql · try it yourself
INSERT INTO categories (name) VALUES ('Garden');

SELECT category_id, name FROM categories ORDER BY category_id DESC LIMIT 2;
1 row affected.

category_id | name
------------+-------
9           | Garden
8           | Books
(2 rows)

Other databases need an explicit keyword for generated ids — see the AUTOINCREMENT lesson.

Composite primary keys

A primary key can consist of several columns; then the combination must be unique. A table recording which supplier can deliver which product:

sql · try it yourself
CREATE TABLE supplier_products (
  supplier_id INTEGER NOT NULL,
  product_id  INTEGER NOT NULL,
  lead_days   INTEGER,
  PRIMARY KEY (supplier_id, product_id)
);

INSERT INTO supplier_products VALUES (1, 1, 3), (1, 2, 3), (4, 2, 7);
SELECT * FROM supplier_products ORDER BY supplier_id, product_id;

INSERT INTO supplier_products VALUES (1, 2, 5);
3 rows affected.

supplier_id | product_id | lead_days
------------+------------+----------
1           | 1          | 3
1           | 2          | 3
4           | 2          | 7
(3 rows)

Error: UNIQUE constraint failed: supplier_products.supplier_id, supplier_products.product_id

Natural vs surrogate keys

Kind Example Pros Cons
Surrogate key customer_id INTEGER generated by the database never changes, compact, simple joins has no meaning outside the database
Natural key ISBN, country code, e-mail meaningful, no extra column may change (e-mail!) or not be truly unique

A common compromise: a surrogate primary key plus a UNIQUE constraint on the natural key.

sql · try it yourself
CREATE TABLE countries (
  country_id INTEGER PRIMARY KEY,
  iso_code   TEXT NOT NULL UNIQUE,
  name       TEXT NOT NULL
);

INSERT INTO countries (iso_code, name) VALUES ('DE', 'Germany'), ('VN', 'Vietnam');
SELECT * FROM countries ORDER BY country_id;
2 rows affected.

country_id | iso_code | name
-----------+----------+--------
1          | DE       | Germany
2          | VN       | Vietnam
(2 rows)

Adding a primary key later

Database Syntax
MySQL, PostgreSQL, SQL Server, Oracle ALTER TABLE t ADD CONSTRAINT pk_t PRIMARY KEY (id);
SQLite not possible with ALTER — define it in CREATE TABLE (or rebuild the table)

Note: For historical reasons SQLite allows NULL in a primary key column that is not INTEGER PRIMARY KEY unless you also declare it NOT NULL. Always write NOT NULL on such columns, as above.

Test yourself

How many primary keys can a table have?

  1. One
  2. Two
  3. One per column
  4. Unlimited

In a composite primary key (supplier_id, product_id), what must be unique?

  1. supplier_id
  2. product_id
  3. The combination of both
  4. Neither