【发布时间】:2014-02-02 17:15:53
【问题描述】:
我已经制作了这个数据库。看起来它工作正常,除了我被告知我的表“事件”不是第三范式。我不明白为什么它不是第三范式。我认为这可能是因为城市和邮政编码应该总是相同的,但是大城市可以有多个邮政编码,我没有看到为城市及其邮政编码创建另一个表的意义,相关到事件表。
如果使用系统保留的某些名称错误地命名了某些名称或属性,也很抱歉。我不得不把代码翻译成英文,因为我是用我的母语写的:)。感谢您的帮助。
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
编辑: 你认为它可以这样工作吗?我创建了另一个名为 location 的表,其中包含以前在事件表中的所有地址信息,并创建了 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:如果不在 3NF 中,则必须存在传递依赖。它在哪里?
标签: sql sql-server normalization database-normalization third-normal-form