This form adds a new constraint to a table using the same constraint
syntax as CREATE TABLE , plus the option NOT
- VALID, which is currently only allowed for foreign key,
- CHECK constraints and not-null constraints.
+ VALID, which is currently only allowed for foreign- key,
+ CHECK , and not-null constraints.
existing rows in the table satisfy the new constraint. But if
the NOT VALID option is used, this
potentially-lengthy scan is skipped. The constraint will still be
- enforc ed against subsequent inserts or updates (that is, they'll fail
+ appli ed against subsequent inserts or updates (that is, they'll fail
unless there is a matching row in the referenced table, in the case
of foreign keys, or they'll fail unless the new row matches the
specified check condition). But the
This form validates a foreign key, check, or not-null constraint that was
previously created as NOT VALID , by scanning the
table to ensure there are no rows for which the constraint is not
- satisfied. If the constraint is not enforced , an error is thrown.
+ satisfied. If the constraint was set to NOT ENFORCED , an error is thrown.
Nothing happens if the constraint is already marked valid.
(See below for an explanation
of the usefulness of this command.)
- Adding an enforced CHECK or NOT NULL
+ Adding a CHECK or NOT NULL
constraint requires scanning the table to verify that existing rows meet the
constraint, but does not require a table rewrite. If a CHECK
- constraint is added as NOT ENFORCED , the valid ation will
- not be performed.
+ constraint is added as NOT ENFORCED , no verific ation will
+ be performed.
- Scanning a large table to verify a new foreign key or check constraint
+ Scanning a large table to verify new foreign-key, check, or not-null constraints
can take a long time, and other updates to the table are locked out
until the ALTER TABLE ADD CONSTRAINT command is
committed. The main purpose of the NOT VALID