Postgresql:表约束和列约束之间的差异

前端之家收集整理的这篇文章主要介绍了Postgresql:表约束和列约束之间的差异前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
摘自pgsql 8.4 docs:“[…]列约束只是在约束只影响一列时使用的符号方便.”
这个有效的sql怎么样呢?

DROP TABLE IF EXISTS test;
CREATE TABLE test(
id integer CONSTRAINT PK_test PRIMARY KEY  CONSTRAINT nenull NOT NULL CHECK (id3>=id2) 
--the check constraint affects two columns none of which is id,id2 integer,id3 integer
);

摘录似乎只适用于PRIMARY KEY和FOREIGN KEY约束,这些约束只应影响声明约束的同一行上的列,如Catcall所述

解决方法

Elsewhere in the docs . .

We say that the first two constraints are column constraints,whereas
the third one is a table constraint because it is written separately
from any one column definition. Column constraints can also be written
as table constraints,while the reverse is not necessarily possible,
since a column constraint is supposed to refer to only the column
it is attached to. (Postgresql doesn’t enforce that rule,but you should follow it if you want your table definitions to work with other database systems.)

猜你在找的Postgre SQL相关文章