Sub topic 13
Naming Constraints for Readable Errors and Easier Changes
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and understand tables, rows, columns, and common data types. You will learn how explicit constraint names make database errors easier to read and schema changes easier to target.
Why constraint names matter
A constraint is a database rule that rejects invalid data. If you do not name a constraint, the database usually creates a system-generated name. That name may look like orders_customer_id_fkey or an unreadable internal value. Names differ between database systems and can change when a table is recreated.
An explicit name gives the rule a stable identifier. The name can appear in an error message, helping you find the violated rule. You also need the name when you later use ALTER TABLE ... DROP CONSTRAINT.
Use a consistent pattern:
pk_<table>for a primary keyuq_<table>_<column>for a unique constraintfk_<child>_<parent>for a foreign keyck_<table>_<rule>for a check constraint
Naming constraints in `CREATE TABLE`
Place a named constraint after the column definitions. This is table-level syntax, even when the rule concerns one column.
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY,
email VARCHAR(255) NOT NULL,
full_name VARCHAR(100) NOT NULL,
CONSTRAINT pk_customers PRIMARY KEY (customer_id),
CONSTRAINT uq_customers_email UNIQUE (email)
);
CREATE TABLE orders (
order_id INTEGER GENERATED ALWAYS AS IDENTITY,
customer_id INTEGER NOT NULL,
total_amount NUMERIC(10, 2) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'new',
CONSTRAINT pk_orders PRIMARY KEY (order_id),
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id),
CONSTRAINT ck_orders_total_nonnegative
CHECK (total_amount >= 0),
CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped', 'cancelled'))
);If an insert uses an unknown customer_id, the database can report that fk_orders_customer was violated. A negative amount can identify ck_orders_total_nonnegative. The exact wording differs by database, but the named rule remains useful.
Changing named constraints
Names make targeted changes possible. For example, add a maximum order amount, then remove that rule later:
ALTER TABLE orders
ADD CONSTRAINT ck_orders_total_limit
CHECK (total_amount <= 1000000);
ALTER TABLE orders
DROP CONSTRAINT ck_orders_total_limit;Adding a constraint checks existing rows. If any row violates the new rule, the ADD CONSTRAINT statement fails. Fix the data first, or create a rule that matches the existing data. Dropping a constraint removes protection, so review the application and data requirements before doing it.
The exact DROP CONSTRAINT syntax and behavior can vary. Check the documentation for your database system, especially when using migrations.
Exercise
Which constraint name should you use for a rule that prevents duplicate usernames in an accounts table?
Show answer
Use uq_accounts_username, for example: CONSTRAINT uq_accounts_username UNIQUE (username).