AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 1761
Accepted
Joril
Joril
Asked: 2011-03-16 13:32:38 +0800 CST2011-03-16 13:32:38 +0800 CST 2011-03-16 13:32:38 +0800 CST

非唯一多列外键

  • 772

我有一个“评论”表,它模拟了一个主题的对话,如下所示:

id serial  
topic_id integer  
parent_comment_id integer
body text

因此,每条评论都引用了它的主题,最终还有它的父评论(如果它不是关于该主题的第一条评论)。
我想添加一个约束,以防止添加主题/父项不匹配的行(例如,通过引用没有所需评论的主题,或者相反地引用错误主题的评论)。
这可能吗?是否需要触发器?

(为了记录,我试过

ALTER TABLE comments ADD FOREIGN KEY (parent_comment_id, topic_id)
                 REFERENCES comments (id, topic_id)

但它抱怨there is no unique constraint matching given keys for referenced table "comments")

postgresql foreign-key
  • 2 2 个回答
  • 10009 Views

2 个回答

  • Voted
  1. Best Answer
    gbn
    2011-03-16T21:27:46+08:002011-03-16T21:27:46+08:00

    您需要在两个(id,topic_id)上添加超级键(唯一索引/约束)。这为您提供了创建外键的“目标”唯一性。在这种情况下,这就像一个 CHECK 约束。

    ALTER TABLE comments ADD 
        FOREIGN KEY (parent_comment_id, topic_id) REFERENCES comments (id, topic_id)
    

    注意: id 仍然是保留模型的主键。即使 id 是串行的,从建模的角度来看,将 PK 更改为 (id,topic_id) 也是错误的

    • 5
  2. SQLRockstar
    2011-03-16T14:20:30+08:002011-03-16T14:20:30+08:00

    尝试

    ALTER TABLE comments ADD FOREIGN KEY (parent_comment_id, topic_id)
        REFERENCES comments (id)
    

    如果你想让这个工作:

    ALTER TABLE comments ADD FOREIGN KEY (parent_comment_id, topic_id)
        REFERENCES comments (id, topic_id)
    

    然后我相信您需要将您的 PK 更改为同时出现在 id 和 topic_id 列上。

    另外,我认为这个链接有助于解释你需要什么:http ://www.postgresql.org/docs/8.1/static/ddl-constraints.html

    • 1

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve