Sub topic 11
When a change is rejected: constraint errors
This chapter assumes you can write basic SELECT queries with WHERE, understand tables, rows, and columns, and know the basic purpose of primary and foreign keys.
A constraint is a database rule that prevents invalid data. When INSERT, UPDATE, or DELETE violates a constraint, the database rejects the statement instead of storing the invalid result.
Common constraint errors
Suppose the tables include these rules:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (id),
total NUMERIC(10, 2) NOT NULL CHECK (total >= 0)
);The constraints protect different properties:
PRIMARY KEYrequires a unique, non-NULLrow identifier.NOT NULLrequires a value.UNIQUEprevents duplicate values, such as two customers with the same email.CHECKrequires a condition to be true.FOREIGN KEYrequires a referenced parent row to exist.
For example, this insert fails if customer 999 does not exist:
INSERT INTO orders (id, customer_id, total)
VALUES (101, 999, 25.00);The customer_id value points to a missing customer. This is a foreign key violation. The database rejects the order rather than creating an order that cannot be connected to a customer.
This statement fails because the total violates the CHECK constraint:
INSERT INTO orders (id, customer_id, total)
VALUES (102, 1, -5.00);This statement fails when email [email protected] is already present:
INSERT INTO customers (id, name, email)
VALUES (4, 'Another Ada', '[email protected]');Diagnosing the failure
An error usually identifies the violated constraint and sometimes includes the table, column, or conflicting value. Read that information first. Then check the data involved.
For a foreign key error, verify the parent row:
SELECT id
FROM customers
WHERE id = 999;For a uniqueness error, find the existing value:
SELECT id, name, email
FROM customers
WHERE email = '[email protected]';For a NOT NULL error, inspect the statement's column list and values. For a CHECK error, compare the value with the condition defined on the table.
A useful pattern is to preview a change before applying it:
SELECT id, customer_id, total
FROM orders
WHERE total < 0;The database can reject changes caused by UPDATE as well as INSERT:
UPDATE orders
SET total = -1
WHERE id = 100;Foreign keys and parent deletion
Deleting a parent row can also violate a foreign key. If orders reference customer 1, this may fail:
DELETE FROM customers
WHERE id = 1;The database must decide what happens to those orders. The foreign key may use an action such as ON DELETE CASCADE, which deletes dependent orders, or ON DELETE SET NULL, which clears the reference. Choose such actions deliberately because cascading deletes can remove many rows.
Exercise
Why does inserting an order with a nonexistent customer_id fail?
Show answer
The foreign key requires customer_id to match an existing customers.id value, so the database rejects the inconsistent order.