Sub topic 15
How Constraints Relate to Indexes
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and understand tables, rows, columns, and common data types.
A constraint protects data correctness. An index improves lookup speed. They often work together, but they are not the same thing.
Constraints that commonly create indexes
Most database systems create an index to enforce a PRIMARY KEY or UNIQUE constraint. The index lets the database quickly check whether a new value already exists.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL
);This table normally receives:
- A unique index for
customer_id, created for the primary key. - A unique index for
email, created for the unique constraint.
The index prevents duplicate values and also helps queries such as:
SELECT customer_id
FROM customers
WHERE email = '[email protected]';The constraint remains the rule. The index is the structure used to enforce or support that rule.
| Constraint | Common index behavior | Main purpose |
|---|---|---|
| `PRIMARY KEY` | Usually creates a unique index | Identifies each row |
| `UNIQUE` | Usually creates a unique index | Prevents duplicate values |
| `FOREIGN KEY` | Usually requires an indexed parent key, but may not index the child column | Protects relationships |
| `CHECK` | Usually creates no index | Validates a row condition |
| `NOT NULL` | Usually creates no index | Requires a value |
Exact behavior depends on the database system. Some systems allow a constraint to use an existing suitable index. Others create their own index automatically.
Foreign keys and child-side indexes
A foreign key checks that a child value exists in a parent table. The referenced parent column must normally be a primary key or have a suitable unique constraint.
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
);
CREATE INDEX idx_orders_customer_id
ON orders (customer_id);The primary key on customers.customer_id supplies the parent-side index. The explicit index on orders.customer_id is optional for foreign-key validity, but often improves joins and queries that find a customer's orders.
It can also reduce the work required when the database checks whether a customer can be deleted. Without this index, the database may scan all orders to look for matching rows.
Composite constraints and index order
A composite primary key or unique constraint creates an index over multiple columns:
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)
);The index is ordered first by order_id, then by product_id. It efficiently supports searches that begin with order_id, such as finding all products in one order. It may not efficiently support a search by product_id alone. A separate index might be useful for that query.
Indexes do not replace constraints
A regular non-unique index does not prevent duplicates. It only organizes values for faster access. A unique index can prevent duplicates, but using a declared UNIQUE constraint communicates the data rule more clearly and lets the database report it as a constraint violation.
Exercise
Does a foreign key automatically create an index on the child column?
Show answer
Usually no. The parent key must be indexed or uniquely constrained, but you should add a child-side index when joins, parent deletion checks, or common searches need it.