简体   繁体   English

SQL多个主键-本地化

[英]SQL multiple primary keys - localization

I am trying to implement some localization in my database. 我正在尝试在数据库中实现某些本地化。

It looks something like this (prefixes only for clarification) 看起来像这样(前缀只是为了澄清)

tbl-Categories tbl-类别
ID ID
Language 语言
Name 名称


tbl-Articles tbl-Articles
ID ID
CategoryID 分类ID

Now, in my tbl-Categories, I want to have primary keys spanning ID and language, so that every combination of ID and language is unique. 现在,在我的tbl类别中,我想拥有跨越ID和语言的主键,以便ID和语言的每种组合都是唯一的。 In tbl-Articles I would like a foreign key to reference ID in categories, but not Language, since I do not want to bind an article to a certain language, only category. 在tbl-Articles中,我希望外键在类别中引用ID,但在语言中不引用,因为我不想将文章绑定到特定的语言,而仅将类别绑定。

Of course, I cannot add a foreign key to part of the primary key. 当然,我不能将外键添加到部分主键中。 I also cannot have the primary key only on the ID of categories, since then there can only be one language. 我也不能仅在类别ID上拥有主键,因为那时只能有一种语言。 Having no primary keys disables foreign keys altogether, and that is also not a great solution. 没有主键将完全禁用外键,这也不是一个好的解决方案。

Do you have any ideas how I can solve this in an elegant fashion? 您有什么想法可以优雅地解决吗?

Thanks. 谢谢。

Given the scenario you need to have one to many relationship established between Category and Language. 在这种情况下,您需要在类别和语言之间建立一对多的关系。 Create 3 tables: 创建3个表:

  1. Category with CategoryID and Name as columns 以CategoryID和Name为列的Category
  2. Language with LanguageID and Name as Columns 语言,语言ID和名称为列
  3. CategoryLanguage with CategoryLanguageId, CategoryID and LanguageID (create a composite primary key on CategoryId and LanguageId which establishes uniqueness) 具有CategoryLanguageId,CategoryID和LanguageID的CategoryLanguage(在CategoryId和LanguageId上创建一个复合主键,以建立唯一性)

You dont have to do anything on the Articles table since ID and CategoryId establishes that an article can be in one of the category but not dependant on language. 您无需在“文章”表上做任何事情,因为ID和CategoryId可以确定文章可以属于该类别之一,但不依赖于语言。

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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