三分表法是最好的選擇,但我會說它需要一些獨特的,不依賴於翻譯的東西。
如果你在SQL Server 2012+上,你可以使用這種情況的順序,但我不能說這是最佳實踐。
rextester:http://rextester.com/RRTJ4439
create sequence dbo.NewsIdSequence as int start with 1 increment by 1;
create table Lang (
id int not null
, name nvarchar(64)
, alias nvarchar(64)
, constraint pk_Lang primary key clustered (id)
, constraint uq_Lang_Name unique (name)
);
create table NewsLanguage (
news_id int not null
constraint df_NewsLanguage_news_id default (next value for dbo.NewsIdSequence)
, lang_id int not null
, title nvarchar(256) not null
, article nvarchar(max) not null
, constraint pk_NewsLanguage primary key clustered (news_id, lang_id)
, constraint fk_langLanguage_lang_id foreign key (lang_id) references lang(id)
);
insert into Lang (id, Name, alias)
select top 3 langid, name, alias
from syslanguages
order by langid;
declare @NextNewsId int;
set @NextNewsId = next value for dbo.NewsIdSequence;
insert into NewsLanguage(news_id, lang_id, title, article)
select @NextNewsId, 0, 'Hello', 'Hello ... '
union all select @NextNewsId, 1, 'Hallo', 'Hallo ... '
union all select @NextNewsId, 2, 'Bonjour', 'Bonjour ...';
set @NextNewsId = next value for dbo.NewsIdSequence;
insert into NewsLanguage(news_id, lang_id, title, article) values
(@NextNewsId, 0, 'Goodbye','Goodbye ...')
, (@NextNewsId, 1, 'Auf Wiedersehen', 'Auf Wiedersehen ...')
, (@NextNewsId, 2, 'Au Revoir', 'Au Revoir ...');
select *
from dbo.NewsLanguage nl
inner join dbo.Lang l on nl.lang_id = l.id
三分表法效果會更好,因爲瑣佩萊德解釋。這裏是一個沒有匈牙利符號的版本:
create table Lang (
id int not null identity (1,1)
, name nvarchar(64)
, alias nvarchar(64)
, constraint pk_Lang primary key clustered (id)
, constraint uq_Lang_Name unique (name)
);
create table News (
id int not null identity (1,1)
, unique_column_of_importance nvarchar(64)
, constraint pk_News primary key clustered (id)
, constraint uq_News_Title unique (unique_column_of_importance)
);
create table NewsLanguage (
news_id int not null
, lang_id int not null
, title nvarchar(256) not null
, article nvarchar(max) not null
, constraint pk_NewsLanguage primary key clustered (news_id, lang_id)
, constraint fk_NewsLanguage_news_id foreign key (news_id) references news(id)
, constraint fk_NewsLanguage_lang_id foreign key (lang_id) references lang(id)
);
你不能,因爲它不是唯一的。主鍵必須是唯一的。您應該使用groupid和lang_id創建一個密鑰。主鍵意味着如果你要求一個具有特定鍵的行,你只能返回一行。 –
PS你不能將'group_id'設置爲自動增量,因爲每行都會得到一個*不同的*值。 –
group_id,lang_id組合將形成主鍵,您可以通過使用序列使group_id成爲「auto inc」值。更好的是,在group_id位於「父」表中的情況下,創建2個表。 –