Sub topic 14
Changing Constraints with `ALTER TABLE`
Assume you know how to create tables and use INSERT, UPDATE, and DELETE. This chapter focuses on changing rules after a table already exists. The examples use PostgreSQL-style syntax. Other database systems may use different names or options.
A constraint is a rule that protects table data. You can add a rule with ADD CONSTRAINT and remove a named rule with DROP CONSTRAINT.
Adding a constraint
Suppose an existing table stores customer notes:
CREATE TABLE customer_notes (
note_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id INTEGER NOT NULL,
note_text VARCHAR(500),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);You can add a named CHECK constraint later:
ALTER TABLE customer_notes
ADD CONSTRAINT ck_customer_notes_text
CHECK (note_text IS NOT NULL AND length(trim(note_text)) > 0);The database checks the new rule against rows already in the table. If any existing row has a missing or blank note_text, the statement fails. Fix the data first, then add the constraint.
UPDATE customer_notes
SET note_text = 'No details provided'
WHERE note_text IS NULL OR length(trim(note_text)) = 0;
ALTER TABLE customer_notes
ADD CONSTRAINT ck_customer_notes_text
CHECK (note_text IS NOT NULL AND length(trim(note_text)) > 0);Adding a UNIQUE constraint follows the same pattern:
ALTER TABLE customer_notes
ADD CONSTRAINT uq_customer_notes_customer_id_note
UNIQUE (customer_id, note_text);This prevents the same customer from having two identical notes. The existing data must already satisfy that rule.
Dropping a constraint
A named constraint can be removed when the rule is no longer appropriate:
ALTER TABLE customer_notes
DROP CONSTRAINT ck_customer_notes_text;The table and its rows remain. Only the rule is removed. Future inserts and updates can once again store values that the deleted rule would have rejected.
Naming and dialect differences
Explicit names make error messages easier to understand and make later changes possible. Without a name, the database generates one, and that generated name can be difficult to find or may differ between systems.
| Change | Typical PostgreSQL form | Important concern |
|---|---|---|
| Add a rule | `ADD CONSTRAINT name ...` | Existing rows are checked |
| Remove a rule | `DROP CONSTRAINT name` | The name must be known |
| Require a column | `ALTER COLUMN col SET NOT NULL` | Existing nulls must be fixed |
| Stop requiring a column | `ALTER COLUMN col DROP NOT NULL` | Syntax varies by system |
Some systems support options such as NOT VALID, which lets you add certain constraints without checking old rows immediately. That can help with large tables, but new or changed rows must still obey the rule. Use such options only when you understand the validation process.
Exercise
What happens if you add a CHECK constraint to a table containing a row that violates the rule?
Show answer
The ALTER TABLE statement normally fails, so you must correct the existing row or use a carefully planned, database-specific validation process.