Database design 数据库规范化问题
我正在尝试做一个时装精品网站。Database design 数据库规范化问题,database-design,Database Design,我正在尝试做一个时装精品网站。 在这个网站上,每件产品(t恤、牛仔裤等)都属于一个集合。每个系列都有外观(t恤、牛仔裤、配饰)。一个产品可以属于一个系列,也可以属于多个外观。如何设计数据库?在产品表中放置一个“集合”字段。它将是集合表的外键(可能只是一个ID和一个名称) 一看能有很多产品吗?如果是,请使用多对多关系(中间表)对此进行建模 编辑: 您将从三个主要表格开始:产品、系列和外观 然后,您将需要连接表:Look Products,Collection Look product ------
在这个网站上,每件产品(t恤、牛仔裤等)都属于一个集合。每个系列都有外观(t恤、牛仔裤、配饰)。一个产品可以属于一个系列,也可以属于多个外观。如何设计数据库?在产品表中放置一个“集合”字段。它将是集合表的外键(可能只是一个ID和一个名称) 一看能有很多产品吗?如果是,请使用多对多关系(中间表)对此进行建模 编辑: 您将从三个主要表格开始:产品、系列和外观
然后,您将需要连接表:Look Products,Collection Look
product
----------
product_id
description
collection
----------
collection_id
name
look
----------
look_id
name
collection_look
---------------
collection_id
look_id
product_collection_look
---------------
product_id (fk1)
collection_id (fk2)
look_id (fk2)
试着这样做:
Products
ProductID int auto increment PK
ProductName string
CollectionID FK to Collections
...more columns if necessary...
Collections
CollectionID int auto increment PK
CollectionName string
...more columns if necessary...
Looks
LookID int auto increment PK
LookName string
...more columns if necessary...
CollectionLooks
CollectionID composite PK,FK to Collections
LookID composite PK, Fk to Looks
ProductLooks
ProductID composite PK, FK to Products
LookID composite PK, Fk to Looks
这里有一个想法。我使用SQL Server语法进行了说明。目前还不完全清楚一个集合可以有一个外观还是多个外观。我假设下面有很多外观,但要改变这一点真的很容易。同样,这只是使用关系表的一个想法,但还有其他同样有效的可能性
create table Product
(
ProductId int not null primary key,
Name varchar(128) not null unique
)
create table Look
(
LookId int not null primary key,
Name varchar(128) not null unique
)
create table Collection
(
CollectionId not null primary key,
Name varchar(128) not null unique
LookId int not null references Look (LookId)
)
create table CollectionLook
(
SurrogateId int not null primary key,
CollectionId int not null references Collection (CollectionId),
LookId int not null references Look (LookId),
constraint CollectionLookConstraint unique (CollectionId, LookId)
-- Change the above constraint if a collection can only have one look.
)
create table ProductCollection
(
SurrogateId int not null primary key,
ProductId int not null references Product (ProductId),
CollectionId int references Collection (CollectionId),
constraint ProductConstraint unqiue (ProductId)
)
create table ProductLook
(
SurrogateId int not null primary key,
ProductId int not null references Product (ProductId),
LookId int not null references Look (LookId),
constraint ProductLookConstraint unique (ProductId, LookId)
)
鉴于:
- 集合包含产品,并且产品“具有”外观李>
- 一个系列也“有”的外观
如果为false(如果集合A包含具有外观Z的产品p,则集合A必须具有外观Z),然后,层次模型也是最合适的。是的,外观可以有许多产品,集合可以有许多外观,但属于某个组的产品可以在集合中各自独立。产品可以各自属于集合。我应该在产品表中输入集合id吗?我不会。对我来说,规范化会说将产品作为一个独立的列表,然后将表链接到分类。每个产品只能有一个名称,是否也将其作为一个独立的列表?有产品链接吗?不,它只是一个关于产品的专栏。同样的道理也适用于产品的收集,只有一个,所以只要把它放在产品上,为它制作一个新的表格是一种浪费。我发现这类事情通常会演变。例如,在某个时候,您想要管理秋季系列或春季系列,而糟糕的是,该产品已经在一个。。。最好已经完成了这个分离,如果需要的话,可以添加一些日期,然后不再重写应用程序,因为你对事情应该如何运作的看法有限。不,这和名字和pk_id不一样。