gpt4 book ai didi

database - (PostgreSQL) "Advanced"检查约束问题

转载 作者:行者123 更新时间:2023-11-29 14:07:55 24 4
gpt4 key购买 nike

我使用 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"

我尝试搜索“如果存在”之类的检查约束,但没有找到任何内容。

任何提示将不胜感激。

最佳答案

我通常是这样的:

table document (id, common, columns, current_revision)
table revision (id, doc_id, content, version)

这意味着该文档与其修订版具有一对多关系,并且与当前修订版具有一对一关系。

这样一来,您始终可以通过简单的连接为当前修订选择一个完整的文档,并且您的文档表中只有一个唯一的行,您可以在其中链接父/子关系,但仍然有版本控制。

关于database - (PostgreSQL) "Advanced"检查约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5209775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com