【问题标题】:Postgres: foreign key vs reference key?Postgres:外键与参考键?
【发布时间】:2022-01-24 18:08:14
【问题描述】:

在 PostgreSQL 中 列级外键和表级外键约束之间是否存在任何概念和/或性能差异 以下哪个表创建更好,为什么在 PostgreSQL 中? 如果两者都相同,为什么不让其中一个被弃用?

    create table game
    (
    gdate date,
    htid int,
    vtid int,
    hscore int default 0,
    vscore int default 0,
    primary key (gdate, htid),
    foreign key (htid) references team,
    foreign key (vtid) references team
    );

create table game
(
    gdate date,
    htid int references team(tid),
    vtid int references team(tid),
    hscore int default 0,
    vscore int default 0,
    primary key (gdate, htid)
);

谢谢

【问题讨论】:

  • 因为表级 FK 允许您构建多列 FK,例如foreign key (some_fld, another_fld) references team(team_fld_1, team_fld_2)
  • 技术上没有区别,你更喜欢哪一个是个人喜好问题

标签: postgresql foreign-keys create-table


【解决方案1】:

“表约束”和“列约束”是产生相同结果的两个语法选项。您可以通过使用任一选项创建一个表并使用psql 中的\d 查看结果或使用pg_dump -s -t table_name dbname 转储元数据来证明这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多