Sub topic 8
Adding and Dropping Constraints
Assume you know basic CREATE TABLE, INSERT, UPDATE, and DELETE, and that you understand primary and foreign keys. This chapter uses PostgreSQL-style syntax. Other database systems use similar ideas but may use different commands for dropping constraints.
A constraint is a rule that the database checks when data changes. Constraints protect relationships and prevent invalid values. Common constraints include:
PRIMARY KEY, which uniquely identifies each row.FOREIGN KEY, which requires a value to exist in another table.UNIQUE, which prevents duplicate values.CHECK, which requires a condition to be true.NOT NULL, which requires a value.
Add a Named Constraint
Use ALTER TABLE when the table already exists. Name each constraint explicitly. A name makes the constraint easier to inspect, change, and remove later.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
total DECIMAL(10, 2) NOT NULL
);
ALTER TABLE orders
ADD CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id);
ALTER TABLE orders
ADD CONSTRAINT chk_orders_total
CHECK (total >= 0);
ALTER TABLE customers
ADD CONSTRAINT uq_customers_name
UNIQUE (name);When you add a foreign key, existing rows are checked immediately in many systems. If an existing orders.customer_id value has no matching customer, the statement fails. Clean or repair the data before adding the constraint.
Drop a Constraint by Name
To remove a constraint, provide the table and its name:
ALTER TABLE orders
DROP CONSTRAINT chk_orders_total;
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer;Dropping the foreign key allows an order to contain a customer_id that does not exist in customers. The column remains, but the relationship is no longer enforced. This can create orphaned data, so remove a constraint only when the application and data model no longer need it.
Some systems support a defensive form:
ALTER TABLE orders
DROP CONSTRAINT IF EXISTS fk_orders_customer;Support for IF EXISTS differs by database and by statement. Check the documentation for your database before using it in a migration.
Change Other Rules
NOT NULL and defaults are usually changed with column-specific syntax rather than DROP CONSTRAINT. For example, PostgreSQL uses:
ALTER TABLE orders
ALTER COLUMN total SET DEFAULT 0;
ALTER TABLE orders
ALTER COLUMN total DROP DEFAULT;
ALTER TABLE orders
ALTER COLUMN total SET NOT NULL;
ALTER TABLE orders
ALTER COLUMN total DROP NOT NULL;Before setting NOT NULL, find and fix existing null values. Before adding a UNIQUE constraint, find duplicate values. The database must be able to enforce the new rule for every existing row.
Plan the Migration
Adding a constraint can fail or lock a table while the database checks its rows. Dropping one can allow invalid data immediately. Test the migration on a copy of production data, record the constraint names, and decide how to recover if the change fails.
Exercise
Why is naming a foreign key constraint useful when you later need to remove it?
Show answer
The name identifies the exact constraint for an ALTER TABLE ... DROP CONSTRAINT statement, avoiding guesswork and making migrations repeatable.