3

我是 PHP/数据库的新手……但我很快就学会了。我想问你们的很简单。我想规范化我的数据库并且不积极如何去做。我明白了这个概念,但看到了多种方法来做到这一点。图我会问有经验的人。

这是我的数据库(到目前为止 2 个表):品牌产品

***Brands Breakdown:***
1   id          int(6)
**Note:** Above, I will probably use 4-Letter codes for each brand instead of primary/int/auto.
2   name            text
3   logo            varchar(20)
4   phone           varchar(20)
5   website     varchar(30)
6   contact_name    text
7   contact_number  varchar(20)
8   contact_email   varchar(30)
9   warehouse       varchar(20)
10  pricing         varchar(15)
11  bio             varchar(300)

***Products Breakdown***
id (INT(6) / Auto_Increment)
brand (This is where I'll insert the four letter code for brand)
category (e.g. Brakes)
subCategory (e.g. Brake Rotors)
details (e.g. Drilled and Slotteed 'Razr')
sku (Part #)
minYear
maxyear
make (e.g. Subaru)
model (e.g. Impreza)
subModel (e.g. WRX STi)
description (Paragraph on part describing it)

specs (I imagine this can be expanded on. need cells somewhere for sizes / colors / engine codes / etc.)

msrp
jobber
price
cost
weight (of part)
warehouse (Could be moved to brand's table)
image (URL of image for the part)

所以我的主要问题是:我是否让每个品牌都有自己的表格,类似于我目前的“产品”表格?还是有“类别”表?“子类别”?你们将如何规范化这些数据?

我想在学习这些东西的同时拥有一个可靠的数据库,这样我才能以正确的方式学习。任何意见,将不胜感激。

更新:对于遇到这个问题并试图学习如何构建数据库的人来说,当我问这个问题时,我没有意识到的一件主要事情是所谓的“基数”。研究这个主题并学习如何将它应用到您的数据库模式中!

4

4 回答 4

2

更改products.brandproducts.brand_id并使其成为 的外键brands.id

创建一个categories表和字段idnameparent_id(允许NULL)将容纳categories.id其父级(NULL表示顶级类别)。或者,您可以使用嵌套集模型。products然后会有一个products.category_id字段(不需要subCategory字段)。

于 2012-05-22T19:35:41.723 回答
2

不要让每个品牌都有自己的桌子。那不是标准化,那是分区。在您的数据库变得非常大之前不要这样做。

目前尚不清楚您的品牌表是什么意思。我猜你的意思是零件制造商,但我不确定。本讨论的其余部分假定您确实指的是零件制造商。

这是我的建议。

重命名您的品牌表。将其称为“制造商”并将其分成两部分,即制造商和联系人。

制造商:

mfrid (your four letter code, primary key)
mfrname            text
mrflogo            varchar(20)
mfrwebsite     varchar(30)
mfrphone           varchar(20)
warehouse       varchar(20)

接触:

mfrid (four letter code)  (part of primary key)
contactid (autoincrement) (part of primary key)
contact_name    text
contact_number  varchar(20)
contact_email   varchar(30)
bio             varchar(300)

为什么“定价”是制造商的属性?“定价”是什么意思?它不是个别部分的属性吗?

将零件表分成两部分。每个部分 sku 都有一个表。另一个将为每个应用程序(即可以使用该零件的每个品牌和型号的汽车)提供一个表格。像这样:

货号:

sku (your stock-keeping unit number, primary key).
mfrid (maker of the PART, not the vehicle in which it fits, foreign key to mfr table).
mfrsku (the brand's stock keeping unit, not necessarily unique in your system)
category (e.g. Brakes)
subCategory (e.g. Brake Rotors)
details (e.g. Drilled and Slotteed 'Razr')
description (Paragraph on part describing it)
saleprice (?)
cost (?)

应用:

ApplicationID (auto incrementing primary key)
make (e.g. Subaru)
model (e.g. Impreza)
subModel (e.g. WRX STi)
firstYear.
lastYear.

然后,您需要一个连接表(因为每个应用程序可以有零个或多个 SKU,反之亦然;也就是说,您的 SKU 和应用程序实体可以有多对多关系)。在您的示例中,您知道多个型号的 Subarus 通常采用相同的部件。该架构允许这样做。

应用SKU:

ApplicationID
SKU

规范化的诀窍是了解您的应用程序域。弄清楚你有什么实体:例如

  1. 德科和斯巴鲁等制造商
  2. 联系像乔和哈利这样的人
  3. 左前雨刮器总成和后雨刮器总成等零件
  4. 1999-2006 Subaru Forester 和 1998-2007 Subaru Impreza 等应用

创建一个与您拥有的每个实体匹配的表。弄清楚您将如何唯一标识每个实体(换句话说,弄清楚您将使用什么作为主键)。

当实体之间存在多对多关系时,请创建连接表。

创建外键以将各种实体连接在一起。

我希望这有帮助。

于 2012-05-22T20:00:44.743 回答
1

请记住,当您到达实际有订单或将物品放入仓库库存的部分时,请存储采取行动时的实际价格。产品的价格是一种查询,它会随着时间而变化,但库存中的订单或商品价值应与输入记录时的实际成本相关。

于 2012-05-22T20:07:23.050 回答
0

一种产品可以安装不止一辆汽车——例如,您可能有一个适合 2010 年丰田凯美瑞、2009 年 Scion tC 和 2011 年讴歌 TL 的雨刮片。因此,您需要从 products 表中拆分 year/make/model,并为车辆(id、year、make、model)和一个连接它们的交叉表(id、product_id、vehicle_id)制作一个单独的表。

于 2012-05-22T19:48:20.953 回答