SQL Server:无法创建关系
我试图创建一个具有一对多关系的表。但似乎在个人中添加外键不起作用。我正在尝试将个人信息表链接到地址表?这个错误的解决办法是什么?
地址
表已成功保存个人
表
无法创建关系“FK_Personal_Address”。
无法创建级联外键“FK_Personal_Address” 引用列“Personal.ID”是一个身份列。不能 创造约束。查看以前的错误。
I was trying to create a table that has a one to many relationships. but it seems that adding a foreign key in Personal is not working. I am trying to link a Personal Information table to a address table? what is the solution for this error?
Address
table saved successfullyPersonal
table
Unable to create relationship 'FK_Personal_Address'.
Cascading foreign key 'FK_Personal_Address' cannot be created where the
referencing column 'Personal.ID' is an identity column. Could not
create constraint. See previous errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Person 表中的主键可能是一个身份。这是一个自动递增的整数字段。
您需要将地址表中的外键设为int类型,而不是identity。它将保存与人员记录相对应的整数,但您不希望外键自动递增。对于子表(地址)中的每条记录,您将为外键设置一个特定值,指示它属于哪个父记录(人员)。
示例:
这将插入新的人员记录,并且
personid
字段将自动填充,因为它是一个 IDENTITY 字段。现在要插入 John Smith 的地址,您需要知道他的
personid
。例如:因此,在
person
表中,personid 是自动生成的,但在address
表中,您指定与现有人员匹配的值。这就是外键的全部意义。如果没有有关架构的更多信息,就很难猜测问题。
The primary key in the Person table is presumably an identity. This is an auto-incrementing integer field.
You need to make the foreign key in the address table of type int, not identity. It will hold integers which correspond to Person records, but you don't want the foreign key to auto-increment. For each record in the child table (address) you will set a specific value for the foreign key indicating to which parent record (Person) it belongs.
Example:
This will insert the new person record and the field
personid
will be filled automatically because it is an IDENTITYfield.Now to insert an address from John Smith you need to know his
personid
. For example:So in the
person
table the personid is generated automatically but in theaddress
table you specify the value that matches an existing person. That's the whole point of a foreign key.Without more information about your schema it's hard to guess the problem.
我确保遵循上面答案中讨论的身份、整数和主键。但是,我仍然遇到同样的错误。
当我将一些数据插入
原因表
(具有主键的表)时,此错误得到解决。如果您读到这里,这可能是您的问题。
I made sure to follow identity, int and primary key discussed in above answer. However, I was still getting the same error.
This error resolved when I inserted some data into a
Reason table.
(table that had a primary key)If you read this far, this might be your problem.
在没有看到问题中表的结构的情况下,我认为最可能的原因是子表中的列(地址)被标记为标识列。在外键关系中,字段的值由父项确定,而不是子项。该列可能是子表中的 PK,但不是 Identity。
Without seeing the structure of the tables in the question, I believe the most likely cause is the column in your child table (Address) is marked as an Identity column. In a foreign-key relationship, the parent determines the value of the field, not the child. The column may be the PK in the child table, but not an Identity.
看来您尝试在 Personal.ID 上创建与其自身相关的外键。
您可能想做类似的事情:
it seem that you try to create a foreign key on Personal.ID related to itself.
You probably want to do something like :
在向我的一张表添加外键约束时,我遇到了同样的错误。
我发现解决方法是添加 NOCHECK。为什么我能够使用 CHECK 添加另外两个外键,但不能添加第三个外键?我发现不是表而是要添加的外键的顺序。任何对此的见解将不胜感激。
I got the same error with adding foreign key constraints to one of my tables.
I found the workaround was to add it WITH NOCHECK. why I was able to add the other two foreign keys WITH CHECK but not the third foreign? I found that it was not the table but the order of the foreign key to be added. Any insight to this will be much appreciated.