数据库设计[item_details,ingredients]

时间:2014-10-22 15:43:23

标签: database-design

我有以下表结构:

项目

ID 名称

ID 成本 item_id_fk

让我们说项目名称是"汉堡包含可乐和炸薯条"。这些是作为项目表中的单个项目处理的3个项目。为了分解项目,我的想法是创建下表:

item_details

ID 名称 item_id_fk

成分

ID 名称 bundle_id_fk

所以在item_details下我可以添加" Burger"," Coke"," French Fries"。然后,对于每个item_details,我可以添加他们各自的成分。

因此它被认为是一个好的设计?

2 个答案:

答案 0 :(得分:1)

嗯..不是真的!商品,价格和细节之间存在一对一的关系。

我认为你需要更像这样的东西......

MEAL表

id  meal_name

MEAL_ITEMS表

id  meal_id  item_id

ITEM表

id   name   price   description

ITEM_INGREDIENTS表

id   item_id   ingredient_id

成分表

id  name

然后是另一张桌子,比如......

这个想法是...... MEALS可以有很多项目...... ITEMS可以有很多成分。
有了这个MySQL,可以轻松地SUM()任何给定MEAL项目的价格。

此查询将显示用餐25中使用的所有成分:

SELECT ig.*
FROM `item_ingredients` as ii,
    `ingredients` as ig,
    `meal_items` as mi,
    'meals` as me
WHERE me.`id` = 25
  AND mi.`meal_id` = me.`id`
  AND ii.`item_id` = mi.`item_id`
  AND ig.`id` = ii.`ingredient_id`

这将显示用餐25的价格总和:

SELECT SUM(it.`price`)
FROM `items` as it,
    `meal_items` as mi,
    'meals` as me
WHERE me.`id` = 25
  AND mi.`meal_id` = me.`id`
  AND it.`id` = mi.`item_id`

答案 1 :(得分:0)

如果这将成为订购系统的一部分,那么您需要使用表继承,以便您可以将订单行项中的外键指向一个表而不使用{{ 3}}

Product

Good is a Product

Burger is a Good

French Fries is a Good

Pop is a Good

Bun is a Good...

Combo is a Product

Combo has many Products

Product belongs to CatalogItem

CatalogItem has a Price

Catalog has many CatalogItems

/* ingredients go here. similar to a combo except a combo can include services, and a BoM can include only goods */

BillOfMaterials belongs to a Good and has many Goods 

您需要了解数据库表继承。

为简单起见,我们将使用单表继承。

架构:

create table product_types (
  product_type_id int primary key,
  name text not null,
  parent_id int null references product_types(product_type_id)
);

create table products (
  product_id int primary key,
  type int not null references product_types(product_type_id),
  name text not null
);

create table combo_products (
  combo_id int references products(product_id),
  product_id int references products(product_id),
  quantity int not null default 1,
  primary key (combo_id, product_id)
);

create table catalogs (
  catalog_id int primary key,
  name text not null 
);

create table catalog_items (
  catalog_id int references catalogs(catalog_id),
  product_id int references products(product_id),
  price numeric(19,2) not null
);

create table bill_of_materials (
  good_id int references products(product_id),
  item_id int references products(product_id),
  quantity int not null default 1,

  primary key (good_id, item_id)
);

用法:

insert into product_types values 
(1, 'Product', null),
(2, 'Good', 1),
(3, 'Combo', 1);

insert into products values
(1,2, 'Burger'),
(2,2, 'Fries'),
(3,2, 'Pop'),
(4,3, 'Combo 1'),
(5,2, 'Bun'),
(6,2, 'Patty'),
...;

insert into bill_of_materials values
(1, 5, 1), /*bun in burger*/
(1, 6, 1)  /*patty in burger*/
...;

insert into combo_products values 
(4, 1, 1), /*burger*/
(4, 2, 1), /*fries*/
(4, 3, 1); /*pop*/

insert to catalog values
(1, 'Default Catalog');

insert into catalog_items values
(1, 1, 4.99), /*burger*/
(1, 2, 2.49), /*fries*/
(1, 3, 1.99), /*pop*/
(1, 4, 7.99); /*combo1*/