我已經創建了這個數據庫。看起來它工作正常,除了我被告知我的表「事件」不是第三範式。我不明白爲什麼它不是第三種正常形式。我認爲這可能是因爲城市和郵政編碼應該始終相同,但大城市可以有多個郵政編碼,而且我沒有看到只爲城市和他們的郵政編碼創建另一個表的重點,相關到事件表。爲什麼我的SQL表格不是3個正常格式
同樣抱歉,如果某些名稱或屬性使用系統保留的某些名稱命名不正確。我不得不把代碼翻譯成英文,因爲我用我的母語寫了它:)。謝謝你的幫助。
Create table [article]
(
[id_article] Integer Identity(1,1) NOT NULL,
[id_author] Integer NOT NULL,
[id_category] Integer NOT NULL,
[title] Nvarchar(50) NOT NULL,
[content] Text NOT NULL,
[date] Datetime NOT NULL,
Primary Key ([id_article])
)
go
Create table [author]
(
[id_author] Integer Identity(1,1) NOT NULL,
[name] Nvarchar(25) NOT NULL,
[lastname] Nvarchar(25) NOT NULL,
[email] Nvarchar(50) NOT NULL, UNIQUE ([email]),
[phone] Integer NOT NULL, UNIQUE ([phone]),
[nick] Nvarchar(20) NOT NULL, UNIQUE ([nick]),
[passwd] Nvarchar(50) NOT NULL,
[acc_number] Integer NOT NULL, UNIQUE ([acc_number]),
Primary Key ([id_author])
)
go
Create table [event]
(
[id_event] Integer Identity(1,1) NOT NULL,
[id_author] Integer NOT NULL,
[name] Nvarchar(50) NOT NULL,
[date] Datetime NOT NULL, UNIQUE ([date]),
[city] Nvarchar(50) NOT NULL,
[street] Nvarchar(50) NOT NULL,
[zip] Integer NOT NULL,
[house_number] Integer NOT NULL,
[number_registered] Integer Default 0 NOT NULL Constraint [number_registered] Check (number_registered <= 20),
Primary Key ([id_event])
)
go
Create table [user]
(
[id_user] Integer Identity(1,1) NOT NULL,
[name] Nvarchar(15) NOT NULL,
[lastname] Nvarchar(25) NOT NULL,
[email] Nvarchar(50) NOT NULL, UNIQUE ([email]),
[phone] Integer NOT NULL, UNIQUE ([phone]),
[passwd] Nvarchar(50) NOT NULL,
[nick] Nvarchar(20) NOT NULL, UNIQUE ([nick]),
Primary Key ([id_user])
)
go
Create table [commentary]
(
[id_commentary] Integer Identity(1,1) NOT NULL,
[content] Text NOT NULL,
[id_article] Integer NOT NULL,
[id_author] Integer NULL,
[id_user] Integer NULL,
Primary Key ([id_commentary])
)
go
Create table [category]
(
[id_category] Integer Identity(1,1) NOT NULL,
[name] Nvarchar(30) NOT NULL,
Primary Key ([id_category])
)
go
Create table [registration]
(
[id_user] Integer NOT NULL,
[id_event] Integer NOT NULL,
Primary Key ([id_user],[id_event])
)
go
Alter table [commentary] add foreign key([id_article]) references [article] ([id_article]) on update no action on delete no action
go
Alter table [article] add foreign key([id_author]) references [author] ([id_author]) on update no action on delete no action
go
Alter table [event] add foreign key([id_author]) references [author] ([id_author]) on update no action on delete no action
go
Alter table [commentary] add foreign key([id_author]) references [author] ([id_author]) on update no action on delete no action
go
Alter table [registration] add foreign key([id_event]) references [event] ([id_event]) on update no action on delete no action
go
Alter table [commentary] add foreign key([id_user]) references [user] ([id_user]) on update no action on delete no action
go
Alter table [registration] add foreign key([id_user]) references [user] ([id_user]) on update no action on delete no action
go
Alter table [article] add foreign key([id_category]) references [category] ([id_category]) on update no action on delete no action
go
編輯: 你認爲它可以這樣工作嗎?我製作了另一張名爲「位置」的表格,其中包含以前在事件表中的所有地址信息,並創建了id_event PFK。
Create table [event]
(
[id_event] Integer Identity(1,1) NOT NULL,
[id_author] Integer NOT NULL,
[name] Nvarchar(50) NOT NULL,
[datr] Datetime NOT NULL,
[number_registered] Integer Default 0 NOT NULL Constraint [number_registered] Check (number_registered <= 20),
Primary Key ([id_event])
)
go
Create table [location]
(
[city] Char(1) NOT NULL,
[id_event] Integer NOT NULL,
[street] Char(1) NOT NULL,
[house_number] Char(1) NOT NULL,
[zip] Char(1) NOT NULL,
Primary Key ([id_event])
)
go
Alter table [event] add foreign key([id_auhtor]) references [author] ([id_author]) on update no action on delete no action
go
Alter table [location] add foreign key([id_event]) references [event] ([id_event]) on update no action on delete no action
go
我想說審稿人是正確的這不是...畢竟你沒有一個不同的郵政編碼,城市和街道表。但是...你想要嗎?完美的標準化並不總是「正確」的方式。 – Liath
兩個事件是否可以在同一地址發生?如果是這樣,我會認爲分離應該是所有的地址列到一個單獨的表中。但是,通常,僅通過查看錶定義,就無法確定表是否處於第三範式。無論他們是否取決於實際的*數據*。 –
@Liath:如果它不在3NF中,則必須有傳遞依賴。它在哪裏? –