5.5.3. Adding a Constraint
5.5.3.添加约束
To add a constraint, the table constraint syntax is used. For example:
使用表约束语法添加约束。例如:
ALTER TABLE products ADD CHECK (name <> '');
ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
To add a not-null constraint, which cannot be written as a table constraint, use this syntax:
添加非空约束,不能使用表级约束,语法如下:
ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
The constraint will be checked immediately, so the table data must satisfy the constraint before it can
be added.
约束检查会立即执行,所以在添加约束之前必须要确保表中的数据满足该约束要求。