我使用PostgreSQL,但我在寻找答案的SQL标准越好。(PostgreSQL的)“高级”检查约束问题
我有以下表“文档” -
Column | Type | Modifiers
------------+------------------------+--------------------
id | character varying(32) | not null
version | integer | not null default 1
link_id | character varying(32) |
content | character varying(128) |
Indexes:
"docs_pkey" PRIMARY KEY, btree (id, version)
ID和link_id是有对方,所以link_id自我引用ID之间联动关系的文件。
问题自带的版本。现在,ID不再是主键(不会是唯一的要么),并且不能由link_id为外键引用 -
my_db=# ALTER TABLE docs ADD FOREIGN KEY(link_id) REFERENCES docs (id) ;
ERROR: there is no unique constraint matching given keys for referenced table "docs"
我试图寻找检查约束的东西,如“是否存在“但没有找到任何东西。
任何尖端将不胜感激。
谢谢,我想接受你的答案只是发现SO每个问题只有一个接受答案的限制。由于我更倾向于jishi的答案(因为它更贴近我的应用程序设计),我所能做的就是投票。对不起。 – Jerry 2011-03-07 09:03:01
@ jishi的回答很好 - 我很高兴你接受了它。您可能需要了解[推迟](http://www.postgresql.org/docs/8.4/static/sql-createtable.html#AEN58820)约束条件才能按照他的建议实施 – 2011-03-07 09:12:57