订单和子订单的数据库设计

时间:2014-03-10 22:42:14

标签: mysql database oracle database-design relational-database

以前,我根据以下信息制作了此数据库架构:一个订单包含许多项目,每个项目都可以属于多个订单。商品有一个定价,可以随时间变化。应记录客户在特定日期购买商品的价格以进行历史报告。

CREATE TABLE item (
    item_id INT AUTO_INCREMENT
    ,item_name VARCHAR(30)
    ,item_list_price DECIMAL(10,2)
    ...
    CONSTRAINT ... PRIMARY KEY (item_id)
);
CREATE TABLE `order` (
    order_id INT AUTO_INCREMENT
    ,order_status VARCHAR(30)
    ,customer_id INT NOT NULL
    ...
    ,CONSTRAINT ... PRIMARY KEY (order_id)
);
CREATE TABLE order_item (
    order_item_id INT AUTO_INCREMENT
    ,order_id INT NOT NULL
    ,item_id INT NOT NULL
    ,order_item_quantity INT
    ,order_item_cost DECIMAL(10,2)
    ,CONSTRAINT ... PRIMARY KEY (order_item_id)
    ,CONSTRAINT ... FOREIGN KEY (order_id) REFERENCES `order` (order_id)
    ,CONSTRAINT ... FOREIGN KEY (item_id) REFERENCES `item` (item_id)
);

然而,我注意到一个项目可以有多个子项目。例如,汉堡包项目可以有一个奶酪子项目,一个培根子项目等。据推测,每个子项目应该能够属于许多项目。奶酪可以吃汉堡包,意大利面等。显然,没有递归的子项目。

实施此方法的最佳方法是什么?

这是不使用继承的一种方法。创建子项表和item_subitem桥表。 item_subitem表包含一个强制性的FK到Item,但是一个可空的FK到Subitem。在Order表中,链接到item_subitem。这不是适当的数据库继承,但在某种程度上模仿它。

CREATE TABLE item (
    item_id INT AUTO_INCREMENT
    ,item_name VARCHAR(30)
    ,item_list_price DECIMAL(10,2)
    ...
    ,CONSTRAINT ... PRIMARY KEY (item_id)
);
CREATE TABLE subitem (
    subitem_id INT AUTOINCREMENT
    ,subitem_name VARCHAR(30)
    ,subitem_list_price DECIMAL(10,2)
    ...
    ,CONSTRAINT ... PRIMARY KEY (subitem_id)
);
-- Note how subitem_id is nullable
CREATE TABLE item_subitem (
    item_subitem_id INT AUTO_INCREMENT
    ,item_id INT NOT NULL
    ,subitem_id INT
    ,CONSTRAINT ... PRIMARY KEY (item_id, subitem_id)
    ,CONSTRAINT ... UNIQUE (item_subitem_id)
    ,CONSTRAINT ... FOREIGN KEY (item_id) REFERENCS item (item_id)
    ,CONSTRAINT ... FOREIGN KEY (subitem_id) REFERENCES subitem (subitem_id)
);

-- Note how this bridge table between order and item_subitem links to item_subitem's unique key
CREATE TABLE order_item_subitem (
    order_item_subitem_id INT AUTO_INCREMENT
    ,order_id INT
    ,item_subitem_id INT
    ,order_item_quantity INT
    ,order_item_cost DECIMAL(10,2)
    ,CONSTRAINT ... PRIMARY KEY (order_item_subitem_id)
    ,CONSTRAINT ... FOREIGN KEY (order_id) REFERENCES `order` (order_id)
    ,CONSTRAINT ... FOREIGN KEY (item_subitem_id) REFERENCES item_subitem (item_subitem_id)
);

这是处理这种情况的最佳方法吗?什么会更好?

最终用户应该能够检查他的历史购买情况(请记住,定价可能会在未来发生变化)。以下是一个订单的示例:

Order # 123456
Customer Name - Matthew Moisen
- Salad        $3
- Custom
   - Hamburger $5
   - Cheese    $1
   - Bacon     $1
- Apple        $1
-----------------
Total         $11

1 个答案:

答案 0 :(得分:1)

您所描述的内容称为"捆绑","组合"或"营销包"。

您可以销售一种产品或一揽子产品。你应该在这里使用抽象/继承。

--using class table inheritance. postgresql syntax (sorry, but it's less verbose).

-- a product is an abstract thing you can sell, ex good, combo, service, warranty.
create table products (
  product_id int primary key
);

create table goods (
  product_id int primary key references products(product_id),
  name text not null unique
  ...other good columns
); 

create table bundles (
  product_id int primary key references products(product_id),
  name text not null unique
  ...other bundle columns
);

--map products to bundles:
create table bundle_products (
  bundle_id int references bundles(product_id),
  product_id int references products(product_id),

  primary key (bundle_id, product_id)
);

您需要使用关系除法(无余数)来防止重复捆绑。使用触发器。

--order line item points at products now:
create table line_items (
  order_id int references orders(order_id),
  display_order smallint not null default 0,
  product_id int not null references products(product_id),
  quantity int not null default 1,
  unit_price decimal(19,2) not null,

  primary key (order_id, display_order)
);