CONSTRAINT is an umbrella term for integrity rules such as PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, and NOT NULL. You can declare a constraint inline on a single column or as a table-level clause that references one or more columns. When a constraint is violated, the database rejects the statement that caused the violation, thus protecting data quality. Constraints can be named explicitly, which simplifies debugging and later alteration. They can also be added or dropped after table creation with ALTER TABLE. Some databases support DEFERRABLE and INITIALLY DEFERRED options, letting you delay enforcement until commit. Understanding constraint behavior is essential when loading bulk data, designing relationships, or tuning performance, because constraint checks add overhead but prevent costly data errors.
constraint_type ENUM
(PRIMARY KEY) - UNIQUE|||FOREIGN KEY|||CHECK|||NOT NULL.column_list
(LIST) - One or more column names affected by the rule.reference_table STRING For FOREIGN KEY
- target table.reference_cols
(LIST) - For FOREIGN KEY|||target columns.expression
(STRING For CHECK) - Boolean condition.DEFERRABLE
(FLAG) - Postgres, Oracle|||allows deferred checking.ON DELETE/UPDATE ACTION ENUM CASCADE
(SET NULL) - RESTRICT, etc.PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, NOT NULL, ALTER TABLE, CREATE TABLE
ANSI SQL-92
PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, and NOT NULL are the most common. Each targets a different aspect of data integrity.
Use ALTER TABLE ... RENAME CONSTRAINT in PostgreSQL or sp_rename in SQL Server. Some systems require dropping and recreating the rule.
Existing rows in the child table probably reference non-existent parent keys. Clean or update the data before adding the constraint.
PRIMARY KEY and UNIQUE create indexes in all major databases. FOREIGN KEY columns are not always indexed automatically; create one manually for better performance.