外键 Foreign keys
Create Foreign Key Relationships
Create a foreign key relationship in Table Designer
Using SQL Server Management Studio
-
In Object Explorer, right-click the table that will be on the foreign-key side of the relationship and click Design.
The table opens in Table Designer.
-
From the Table Designer menu, click Relationships. 这里需要先右键选中column,然后才会有menu
-
In the Foreign-key Relationships dialog box, click Add.
The relationship appears in the Selected Relationship list with a system-provided name in the format FK_<tablename>_<tablename>, where tablename is the name of the foreign key table.
-
Click the relationship in the Selected Relationship list.
-
Click Tables and Columns Specification in the grid to the right and click the ellipses (...) to the right of the property.
-
In the Tables and Columns dialog box, in the Primary Key drop-down list, choose the table that will be on the primary-key side of the relationship.
-
In the grid beneath, choose the columns contributing to the table's primary key. In the adjacent grid cell to the left of each column, choose the corresponding foreign-key column of the foreign-key table.
Table Designer suggests a name for the relationship. To change this name, edit the contents of the Relationship Name text box.
-
Choose OK to create the relationship.
SQL Server 2008: The columns in table do not match an existing primary key or unique constraint
问题
I need to make some changes to a SQL Server 2008 database.
This requires the creation of a new table, and inserting a foreign key in the new table that references the Primary key of an already existing table. So I want to set up a relationship between my new tblTwo, which references the primary key of tblOne.
However when I tried to do this (through SQL Server Management Studio) I got the following error:
The columns in table 'tblOne' do not match an existing primary key or UNIQUE constraint
I'm not really sure what this means, and I was wondering if there was any way around it?
回答
It means that the primary key in tblOne hasn't been properly declared - you need to go to tblOne and add the PRIMARY KEY constraint back onto it.
If you're sure that tblOne does have a PRIMARY KEY constraint, then maybe there are multiple tblOne tables in your DB, belonging to different schemas, and your references clause in your FK constraint is picking the wrong one.
If there's a composite key (which your comment would indicate), then you have to include both columns in your foreign key reference also. Note that a table can't have multiple primary keys - but if it has a composite key, you'll see a key symbol next to each column that is part of the primary key.
把B表的一个字段,设置为A表某个字段的外键,前提是B表中的那个字段是主键,或者有唯一性约束
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-03-14 pass an instance of class to TestCase as parameter
2018-03-14 How to Integrate .NET Projects with Jenkins
2016-03-14 Knowing When to Use Override and New Keywords (C# Programming Guide)
2016-03-14 Versioning with the Override and New Keywords (C# Programming Guide)
2016-03-14 Polymorphism (C# Programming Guide)
2015-03-14 2-Medium下的MultipleCommandAssembly
2015-03-14 如何获取supersocket的源代码