Sub topic 6
Identifying Rows with PRIMARY KEY
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and you understand tables, rows, columns, and common data types. This chapter uses PostgreSQL-style SQL. Other database systems use similar primary-key rules, but identity-column syntax can differ.
What a primary key guarantees
A primary key is a column, or group of columns, that identifies one row uniquely. The database enforces two rules:
- Every primary-key value must be unique.
- No primary-key value can be
NULL, which means “missing” or “unknown.”
Each table can have one primary key. A primary key can contain one column or several columns. A table with a primary key is easier to reference from other tables and safer to update or delete.
| Key design | Example | Meaning |
|---|---|---|
| Single-column key | `customer_id` | One value identifies each customer |
| Composite key | `(order_id, product_id)` | The pair identifies each row |
A single-column primary key
Use a stable identifier rather than a value that may change, such as a person’s name or email address.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL,
full_name VARCHAR(100) NOT NULL
);The database rejects this row because its key is missing:
INSERT INTO customers (customer_id, email, full_name)
VALUES (NULL, '[email protected]', 'Ada Lovelace');It also rejects a second row with the same customer_id:
INSERT INTO customers (customer_id, email, full_name)
VALUES (1, '[email protected]', 'Grace Hopper');
INSERT INTO customers (customer_id, email, full_name)
VALUES (1, '[email protected]', 'Alan Turing');The second insert violates the primary-key constraint. The first row remains unchanged.
Composite primary keys
A composite primary key uses multiple columns together. Each individual value may repeat, but the combination must be unique.
This design is useful for a junction table. A junction table represents a many-to-many relationship, such as products belonging to orders.
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);These rows are valid because the pairs differ:
INSERT INTO order_items (order_id, product_id, quantity)
VALUES
(10, 3, 2),
(10, 7, 1),
(11, 3, 4);A second (10, 3) row would be rejected. If an order can contain the same product on separate lines, use a separate generated line identifier instead of this composite key.
Generated key values
An identity column lets the database generate numeric key values.
CREATE TABLE products (
product_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(10, 2) NOT NULL
);You normally omit product_id when inserting:
INSERT INTO products (name, price)
VALUES ('Notebook', 8.50);The generated number is still unique, but identity generation does not replace the primary-key constraint. The primary key is what enforces uniqueness and disallows NULL.
Primary keys and indexes
Most database systems create or use a unique index to enforce a primary key. This helps the database find rows by their key, but the index is an implementation detail. Define the primary key for data integrity, not merely for query speed.
Exercise
Can two rows in a table with PRIMARY KEY (orderid, productid) have the same order_id?
Show answer
Yes. They can have the same orderid when their productid values differ. The pair of values must be unique.