[英]SQL Server Management Studio does NOT let me create multiple foreign keys to multiple primary keys
I have a table ApplicationRegion
in a one-to-many relationship to Translation
. 我有一个表
ApplicationRegion
在一个一对多的关系Translation
。
I want to set FK's from Translation.appname/isocode
to ApplicationRegion.appname/isocode
我想将FK从
Translation.appname/isocode
为ApplicationRegion.appname/isocode
But that did not work as you can see from the screenshot. 但是,从屏幕截图中可以看到,这没有用。
When the FK configure window opens 3 columns are shown on the left/right side 当FK配置窗口打开时,左侧/右侧显示3列
appname
isocode
resourcekey
Then I chose as parent table ApplicationRegion
and removed the resource key column from the FK setting. 然后,我选择作为父表
ApplicationRegion
并从FK设置中删除了资源键列。
And clicked OK but then I got the error you see on the screenshot. 然后单击“确定”,但随后出现屏幕截图中显示的错误。
Finally I made it work with that workaround and I would like to know why I had to use this workaround? 最终,我将其与该替代方法一起使用,并且我想知道为什么必须使用此替代方法?
WHY this workaround? 为什么使用此解决方法?
UPDATE UPDATE
HAHA... 哈哈...
I think you must have hit a weird glitch in SSMS. 我认为您一定在SSMS中遇到了奇怪的问题。 I was able to create your schema using SSMS 2014 without any errors.
我能够使用SSMS 2014创建您的架构,而没有任何错误。 It did pre-fill the three composite primary key columns when adding the new FK.
在添加新的FK时,它确实预填充了三个复合主键列。 I was careful to make sure they were all blanked out before I started to add the two columns in the FK.
在开始在FK中添加两列之前,我要小心确保将它们全部清空。 Maybe SSMS thought one of the blank rows still had data in it.
也许SSMS认为空白行之一中仍然有数据。
Edit: Just had one more thought, SSMS is know for caching any changes that are made when editing a table. 编辑:再想一想,SSMS以缓存在编辑表时所做的任何更改而闻名。 For example, if you go to modify two tables and have both edit windows open.
例如,如果您要修改两个表并同时打开两个编辑窗口。 Then you change the PK in one window and then try to reference it in the second window, it will error because it has cached what the schema was for the first table when the window was first opened.
然后,您在一个窗口中更改PK,然后尝试在第二个窗口中引用它,这将出错,因为它已在第一次打开窗口时缓存了第一个表的架构。
Here is my generated DDL: 这是我生成的DDL:
CREATE TABLE [dbo].[AppRegion](
[appname] [nvarchar](50) NOT NULL,
[isocode] [char](5) NOT NULL,
CONSTRAINT [PK_AppRegion] PRIMARY KEY CLUSTERED
(
[appname] ASC,
[isocode] ASC
)
) ON [PRIMARY]
CREATE TABLE [dbo].[Translation](
[ResourceKey] [nvarchar](128) NOT NULL,
[appname] [nvarchar](50) NOT NULL,
[isocode] [char](5) NOT NULL,
[text] [nvarchar](400) NULL,
CONSTRAINT [PK_Translation] PRIMARY KEY CLUSTERED
(
[ResourceKey] ASC,
[appname] ASC,
[isocode] ASC
)
) ON [PRIMARY]
ALTER TABLE [dbo].[Translation] ADD CONSTRAINT [FK_Translation_AppRegion] FOREIGN KEY([appname], [isocode])
REFERENCES [dbo].[AppRegion] ([appname], [isocode])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.