微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐