加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 15:58:29 所属栏目:百科 来源:网络整理
导读:摘自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 co
摘自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.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读