简体   繁体   English

SQL Server Management Studio不允许我为多个主键创建多个外键

[英]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/isocodeApplicationRegion.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? 最终,我将其与该替代方法一起使用,并且我想知道为什么必须使用此替代方法?

  1. Remove PK from Translation.ResourceKey column 从Translation.ResourceKey列中删除PK
  2. Open FK configure window 打开FK配置窗口
  3. Only 2 columns appear now as foreign keys 现在只有2列显示为外键
  4. I click OK 我点击确定
  5. Now I add the PK again to Translation.ResouceKey column 现在,我再次将PK添加到Translation.ResouceKey列
  6. I added test data to all 3 tables and everything is fine. 我将测试数据添加到所有3个表中,一切都很好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM