【发布时间】:2011-07-09 17:57:40
【问题描述】:
我使用 PostgreSQL,但我正在寻找尽可能标准的 SQL 答案。
我有下表“文档”--
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"
我试图搜索“如果存在”之类的检查约束,但没有找到任何东西。
任何提示将不胜感激。
【问题讨论】:
标签: database postgresql foreign-keys constraints database-schema