外国关键参考交界表创建问题

发布于 2025-01-27 15:14:32 字数 163 浏览 1 评论 0原文

我有两个桌子在两者之间,因为事实之间有很多关系。我如何将外键参考从一个表到接线表获得特定ID(相对表的主要密钥ID),ID是由接线表的主要密钥的一部分两个桌子的两个外键。我将外键参考直接引入交界表,但它抛出了一个错误:“外键的参考列列表中的列数与引用表中的主要键的列数不匹配” 。我该怎么办才能成功地提及交界表?

I have two tables that have a junction table in between due to fact that there is many to many relationship between. How do I make a foreign key reference from one of the tables to the junction table to obtain a specific ID(the primary key ID of the opposite table), the ID is part of the primary key of the junction table that is made up of two foreign keys of the two tables. I made a foreign key reference straight to the junction table but its throwing an error "The number of columns in the referencing column list for foreign key does not match those of the primary key in the referenced table" when launching the CREATE query for the tables. What should I do to successfully make a foreign key reference to junction table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花间憩 2025-02-03 15:14:32

这两种关系的许多 将在接口表中(用SQL编写)

create table t1 (
  id int primary key
) ;

create table t2 (
  id int primary key
) ;

create table junction (
  t1id int references t1( id ) not null
, t2id int references t2( id ) not null
) ;

-- PK: 2 columns
alter table junction
add constraint pkey_j primary key( t1id, t2id ) ;

DBFIDDLE 在这里

The many side of both relationships will be at the junction table eg (written in SQL)

create table t1 (
  id int primary key
) ;

create table t2 (
  id int primary key
) ;

create table junction (
  t1id int references t1( id ) not null
, t2id int references t2( id ) not null
) ;

-- PK: 2 columns
alter table junction
add constraint pkey_j primary key( t1id, t2id ) ;

DBfiddle here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文