ALTER TABLE ADD CHECK
Attaches a rule that every row must satisfy, old and new. All five engines now take the same clause; the differences are which MySQL and SQLite versions mean it and how to skip the big scan.
- Catégorie
- Tables
- In this category
- 11 of 16
Syntaxe
ALTER TABLE t ADD CONSTRAINT ck_name CHECK (condition) -- SQL Server, PostgreSQL, MySQL 8.0.16+, SQLite 3.53.0+, Oracle
ALTER TABLE t ADD CONSTRAINT ck_name CHECK (condition) NOT VALID -- PostgreSQL: scan old rows later
-- SQLite before 3.53.0: rebuild the table to add one
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | validates existing rows unless you add WITH NOCHECK, which stores the constraint untrusted |
| PostgreSQL | Oui | the plain form scans under a strong lock; NOT VALID plus VALIDATE CONSTRAINT keeps writes running |
| MySQL | Different syntax | enforced from 8.0.16; older servers accepted the clause and ignored it |
| SQLite | Different syntax | ALTER TABLE ADD CONSTRAINT ... CHECK arrived in 3.53.0 and checks existing rows; older versions rebuild the table |
| Oracle | Oui | ENABLE NOVALIDATE skips existing rows; every future write is checked |
Exemples
PostgreSQL
ALTER TABLE orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped'));
PostgreSQL
-- add without the scan, validate later while writes continue
ALTER TABLE orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped')) NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT ck_orders_status;
SQL Server
ALTER TABLE dbo.orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped'));
MySQL
-- requires 8.0.16 or newer; older servers ignore CHECK
ALTER TABLE orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped'));
Oracle
-- skip the scan of old rows
ALTER TABLE orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped')) ENABLE NOVALIDATE;
SQLite
-- requires 3.53.0 or newer; older versions rebuild the table
ALTER TABLE orders ADD CONSTRAINT ck_orders_status
CHECK (status IN ('new', 'paid', 'shipped'));
Bon à savoir
- A CHECK sees one row and nothing else: every engine rejects subqueries and references to other rows inside the condition. A rule that needs another table is a foreign key's job.
- MySQL's history bites quietly: before 8.0.16 the parser accepted CHECK and the server never enforced it, so parsed and enforced are different claims there. Run SELECT VERSION() before trusting one.
- The row scan is the expensive part. PostgreSQL's VALIDATE CONSTRAINT step re-scans but takes only SHARE UPDATE EXCLUSIVE, so reads and writes continue. SQL Server's WITH NOCHECK and Oracle's NOVALIDATE skip old rows entirely and enforce the rule only on new writes.
- The condition passes NULLs: in SQL's three-valued logic a condition over NULL comes out unknown, and CHECK accepts unknown. CHECK (price > 0) lets NULL prices through; add NOT NULL to the column to close that door.
Questions about ADD CHECK
Why is my CHECK constraint ignored on MySQL?
Servers older than 8.0.16 parse CHECK constraints and never enforce them, a leftover from the old parser. The clause returns success either way, so verify with SELECT VERSION() and test one deliberately bad insert.
Can a CHECK query another table?
No, on every engine. The condition may only read the current row, and subqueries are rejected outright. For cross-table rules use a foreign key, a trigger, or validation in the application.
Does the CHECK apply to NULLs?
It passes them: unknown satisfies a CHECK. CHECK (price > 0) therefore allows a NULL price on every engine, which surprises people who wrote the rule as a range check. Declare the column NOT NULL when NULL is not a legal value.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.