Sub topic 8
Preventing Duplicates with `UNIQUE`
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and that tables contain rows and columns. It also assumes you recognize common data types such as integers, text, and dates.
A UNIQUE constraint is a database rule that prevents two rows from having the same value, or the same combination of values, in selected columns. It protects data even when several applications write to the same table.
Enforcing uniqueness in one column
Use a column-level constraint when one column must contain a distinct value in every row. Email addresses, usernames, and external reference codes often need this rule.
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL,
full_name VARCHAR(100) NOT NULL,
CONSTRAINT uq_customers_email UNIQUE (email)
);
INSERT INTO customers (email, full_name)
VALUES ('[email protected]', 'Ada Lovelace');
-- Rejected: another row would have the same email.
INSERT INTO customers (email, full_name)
VALUES ('[email protected]', 'Another Ada');The second INSERT fails because the value already exists. The database rejects the whole statement, so it does not create a duplicate row.
UNIQUE does not automatically mean NOT NULL. If a column may contain NULL, the exact handling of multiple NULL values depends on the database system. In many systems, several NULL values are allowed because NULL means “unknown,” not a specific value. Add NOT NULL when every row must provide a value.
Enforcing unique combinations
Sometimes a value may repeat by itself but must be unique when combined with another value. For example, a store can use the same product code in different warehouses, but not twice in the same warehouse.
CREATE TABLE warehouse_products (
warehouse_id INTEGER NOT NULL,
product_code VARCHAR(40) NOT NULL,
quantity INTEGER NOT NULL,
CONSTRAINT uq_warehouse_product
UNIQUE (warehouse_id, product_code)
);The constraint checks the pair (warehouse_id, product_code). These rows are valid:
| warehouse_id | product_code | quantity |
|---|---|---|
| 1 | A-100 | 12 |
| 2 | A-100 | 8 |
The product code repeats, but each pair is different. A second row with (1, 'A-100') is rejected.
Naming and changing a `UNIQUE` constraint
Name constraints explicitly. A meaningful name makes database errors easier to understand and lets you refer to the rule in an ALTER TABLE statement.
ALTER TABLE customers
ADD CONSTRAINT uq_customers_email UNIQUE (email);
ALTER TABLE customers
DROP CONSTRAINT uq_customers_email;The ADD CONSTRAINT statement fails if existing rows already contain duplicates. Clean up or merge those rows before adding the rule. Dropping the constraint allows future duplicates, so do it only when that change matches the data model.
A database commonly supports a unique constraint with a unique index. The constraint expresses the data rule; the index helps the database find duplicates efficiently. The exact index name and management behavior vary by system.
Exercise
In the warehouseproducts table, will a row with warehouseid = 2 and product_code = 'A-100' be rejected when the existing rows are (1, 'A-100') and (2, 'B-200')?
Show answer
No. The pair (2, 'A-100') does not yet exist, so it satisfies the composite UNIQUE constraint.