从一列到另一列的外键包含在多列主键中

时间:2015-04-17 12:27:02

标签: sql sql-server sql-server-2012

我尝试在一列到两列主键之间创建一个外键,而SQL Server拒绝这样做。为什么?

我有一张桌子用不同语言和国家/地区表格存储我的措辞。

    ref.SystemLabels
    --------------
--> [Id] int
|   [IdLanguage] int
|   Label nvarchar(200)
|   Keywords nvarchar(200)
|   
|   ref.Countries
|   --------------
|   [Id]
--> IdSystemLabel
    IsoCode

在我看来,这是非常逻辑但我不明白为什么SQL Server不理解或接受我的逻辑^^。

如果有人可以帮我这个。

注意我认为我会以另一种方式在IdSystemLabel上创建索引,尽管有外键。


编辑:正如您问我的那样,请查看表格的SQL代码

表格SystemLanguages

CREATE TABLE [ref].[SystemLanguages](
    [Id] [int] NOT NULL,
    [IdSystemLabel] [int] NOT NULL,
    [IsoCode] [nchar](2) NOT NULL,
    [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_SystemLanguages] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [ref].[SystemLanguages] ADD  CONSTRAINT [DF_SystemLanguages_Enabled]  DEFAULT ((0)) FOR [Enabled]
GO

表格SystemLabels

CREATE TABLE [ref].[SystemLabels](
    [Id] [int] NOT NULL,
    [IdLanguage] [int] NOT NULL,
    [Label] [nvarchar](max) NOT NULL,
    [Keywords] [nvarchar](200) NULL,
    [CreatedAt] [datetime] NOT NULL,
    [UpdatedAt] [datetime] NULL,
    [Group] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_SystemLabels] PRIMARY KEY CLUSTERED 
(
    [Id] ASC,
    [IdLanguage] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [ref].[SystemLabels] ADD  CONSTRAINT [DF_SystemLabels_CreatedAt]  DEFAULT (getdate()) FOR [CreatedAt]
GO

注意由于循环依赖性,此表不能具有SystemLanguages的外键

表格LocalizationLevel0

CREATE TABLE [ref].[LocalizationLevel0](
    [Id] [int] NOT NULL,
    [IdSystemLabel] [int] NOT NULL,
    [IsoCode] [nchar](2) NOT NULL,
    [CreatedAt] [datetime] NOT NULL,
    [UpdatedAt] [datetime] NULL,
    [Enabled] [bit] NULL,
 CONSTRAINT [PK_LocalizationLevel0] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [ref].[LocalizationLevel0] ADD  CONSTRAINT [DF_LocalizationLevel0_CreatedAt]  DEFAULT (getdate()) FOR [CreatedAt]
GO

ALTER TABLE [ref].[LocalizationLevel0] ADD  CONSTRAINT [DF_LocalizationLevel0_Enabled]  DEFAULT ((0)) FOR [Enabled]
GO

我试图解释我的逻辑:

一个 localizationlevel0 项目可以使用多种系统语言进行翻译:

法国国家的例子

1 4 'FR' '2015-04-17 00:00:00:000' NULL 1

- 系统语言

1 1 'FR' 1
2 2 'EN' 1
3 3 'DE' 1

- 相应的措辞

1 1 'Français' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'
1 2 'French' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'
1 2 'Französisch' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'

...
4 1 'France' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'
4 2 'France' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'
4 3 'Frankreich' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'

2 个答案:

答案 0 :(得分:2)

您必须始终引用整个主键 因此,如果第一个表具有复合主键(Id, IdLanguage),则第二个表必须还具有复合外键(SystemLabels.Id, SystemLabels.Language)只有(SystemLabels.Id)。< / p>

答案 1 :(得分:1)