[英]Sql Server - Foreign key referencing multiple columns one of which isn't in the source table
I've got a problem with foreign key. 我的外键有问题。 This is my DB structure (simplified): 这是我的数据库结构(简化):
Table 'Languages' 表“语言”
LanguageID - primary key
LanguageName - string (for example 'English')
..
Table 'Users' 表“用户”
UserID - primary key
LanguageID - byte (FK to Languages.LanguageID)
..
Table 'Localization' 表“本地化”
LocalizationID - / compound primary key
LanguageID - \ compound primary key (FK to Languages.LanguageID)
Data - string (for example 'My Program' in English)
Table 'UserLocalization' 表“ UserLocalization”
UserLocalizationID - primary key
UserID - which user (FK to Users.UserID)
..
some other useful columns -> this table cannot be removed **EDITED**
..
LocalizationID - what string (FK to Localization.?) <- oops, not really because
LanguageID is needed
for FK to 'Localization'
How to make a FK (or any other integrity check) in 'UserLocalization' to 'Localization'. 如何在“ UserLocalization”到“ Localization”中进行FK(或任何其他完整性检查)。 Is it possible in this configuration ? 在这种配置下可以吗? Or is it not ok, and therefore some restructuralization (really ?) is needed ? 还是不行,因此需要一些重组(真的吗?)? If so how to accomplish it ? 如果是这样,如何完成呢?
Edit: A bit cleaned up for better clarity. 编辑:进行了一些清理,以提高清晰度。
The simple answer is: if you have your table Localization
like this: 简单的答案是:如果您的表Localization
像这样:
LocalizationID - / compound primary key
LanguageID - \ compound primary key (FK to Languages.LanguageID)
Data - string (for example 'My Program' in English)
and you want to add a foreign key to another table referencing this table, that foreign key needs to have both columns of your PK here at hand, so it could be: 并且您想向该表引用的另一个表中添加一个外键,该外键需要在此处拥有您的PK的两列 ,因此可能是:
Table 'UserLocalization' 表“ UserLocalization”
UserLocalizationID - primary key
UserID - which user (FK to Users.UserID)
(LocalizationID, LanguageID) - FK to Localization
That's one of the downsides of compound primary keys - any FK referencing them must also include all columns of the compound PK - no exceptions / tricks / workarounds possible. 这是复合主键的缺点之一-任何引用主FK的FK还必须包括复合PK的所有列-不可能有任何异常/技巧/变通办法。 With two columns, that's still doable, but with four, five, ten columns it gets really really messy. 如果有两列,那仍然可行,但是有了四,五,十列,它真的很混乱。 It also means any JOIN
to that table must contain all common fields - and again, with two it's still ok, but with more, it gets really messy. 这也意味着到该表的任何JOIN
必须包含所有公共字段-再说一次,两个仍然可以,但是更多的话,它会变得非常混乱。
This is one of the reasons I would often consider adding an artificial surrogate key to tables where I only have a compound PK - just to simplify FK joins to it. 这就是我经常考虑在只有复合PK的表中添加人工代理键的原因之一,这只是为了简化FK联接。
Get rid of the UserLocalization
table. 摆脱UserLocalization
表。 Use this SQL instead to find the localized strings for the users: 请改用以下SQL查找用户的本地化字符串:
SELECT * FROM Users INNER JOIN Localization ON Users.LanguageID = Localization.LanguageID
All users with the same language need/have the same localization records, so you don't need to add any more integrity checks; 使用相同语言的所有用户都需要/具有相同的本地化记录,因此您无需添加更多完整性检查; you're doing all the checks already with the FKs on LanguageID
in the Users
and Localization
tables. 您已经使用“ Users
和“ Localization
表中的LanguageID
上的FK进行了所有检查。
If you want to find a specific user's localized data/strings just put a WHERE UserID = [Whatever]
on the end of the SQL. 如果要查找特定用户的本地化数据/字符串,只需在SQL末尾添加WHERE UserID = [Whatever]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.