将两个外键放入单个列中
我在创建一个表时遇到问题,该表的列具有两个外键关系。让我详细解释一下。
我有三个表:
表 A
- 列ID(主键)、名称
表 B
- 列ID(主键)、名称
表 C
- 列ID、名称、详细信息
在 C.Detail
中,我必须存储来自其他两个表的数据(A .ID 和 B.ID)。所以我尝试在C.Detail
列中添加两个外键。在表 B
中进行插入操作期间,会发生以下错误,并且在尝试将数据插入表 A
时也会出现相同的错误消息。
“INSERT 语句与 FOREIGN KEY 约束冲突 “FK_C_A”。冲突发生在数据库“X”,表“dbo.A”,列中 A.ID。”
请问谁能帮助我们解决这个问题吗?我们不想在表 C
中为两个外键添加两列。
希望等待回复。
I have a problem in creating a table with a column that has two foreign Key relationships. Let me explain in detail.
I have three tables:
Table A
- columnsID (primary key), Name
Table B
- columnsID (primary key), Name
Table C
- columnsID, Name, Detail
In C.Detail
I have to store data from both other tables (A.ID & B.ID). So I tried to add two foreign key into the column C.Detail
. During insert operation in Table B
the following error occurs, and the same error message occurs while trying to insert data into Table A
.
"The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_C_A". The conflict occurred in database "X", table "dbo.A", column
A.ID."
Please, can anyone help us to rectify this problem? We don't want to add two columns in table C
for two foreign keys.
Hopefully waiting for the reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在表 C 中引入两个新列(即 AID 和 BID)。
在此新闻栏目上创建 Foregin 键。
I will suggest to introduce two new columns in Table C. (i.e AID and BID).
Create Foregin key on this news columns.
我可能是错的,但我认为这样做的方法是为 A 和 B 创建一个具有 A_B_parent.id(primary_key) 的“父”表,然后让 A 和 B 的 id 上都有外键父表。那么C也可以有父表的外键。
显然,这最终变得非常复杂,因此更好的解决方案可能只是以编程方式强制执行约束,而不是使用外键,然后在表上添加注释。
I could be wrong, but I think the way to go about doing this is to create a "parent" table for A and B that has A_B_parent.id(primary_key) and then have A and B both have foreign keys on their id to the parent table. Then C can also have a foreign key to the parent table.
This obviously ends up being really complex, so the better solution might just be to programmatically enforce the constraint rather than using foreign keys and then put a comment on the table.