创建没有主键的外键 [英] create foreign key without a primary key

查看:191
本文介绍了创建没有主键的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有必要在一个表的列上有一个主键,而另一个表的列有外键引用。

  create table D(Did int)
创建表E(Eid int外键引用D(Did))

上面的查询给出错误:

 被引用表'D'中没有与外键FK__E__Eid__79C80F94中的引用列表匹配的
的主键或候选键。


解决方案

如果在父表中有两个相同的值,那么如何知道将哪个子行关联到?
外键的一边必须是明确的

要求也是唯一键,不仅仅是一个主键当然是唯一的


Why is it necessary to have a primary key on a column of one table to which a column of the other table having foreign key references.

create table D(Did int)
create table E(Eid int foreign key references D(Did))

The above query gives error:

There are no primary or candidate keys in the referenced table 'D' that match
the referencing column list in the foreign key 'FK__E__Eid__79C80F94'.

解决方案

Easy. If you have 2 values the same in the parent table, how do you know which one to associate child rows to? One side of foreign key must be unambiguous

The requirement is also "unique key", not just a primary key which of course unique

这篇关于创建没有主键的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆