一般数据库规范化

时间:2015-01-09 03:25:12

标签: sql database-normalization

假设我有一张我卖给客户的产品表。

每条记录都有productID和productName。

我可以向每位客户销售超过1种产品,但我希望允许客户只订购某些产品。

这些表格会是什么样的?

这是我到目前为止所做的:

PRODUCTS
+------------+-------------+
| PROD_ID    |  PROD_NAME  |
+------------+-------------+

CUSTOMER
+------------+-------------+
| CUST_ID    |  CUST_NAME  |
+------------+-------------+

ORDERS
+------------+-------------+
| ORDER_ID   |  CUST_ID    |
+------------+-------------+

1 个答案:

答案 0 :(得分:1)

我用PostgreSQL编写并测试了这个,但是对于任何SQL dbms,原则都是一样的。

产品和客户的表格很简单。

create table products (
  prod_id integer primary key,
  prod_name varchar(35) not null
);
insert into products values 
(1, 'Product one'), (2, 'Product two'), (3, 'Product three');

create table customers (
  cust_id integer primary key,
  cust_name varchar(35) not null
);
insert into customers values 
(100, 'Customer 100'), (200, 'Customer 200'), (300, 'Customer 300');

表" permits_products"控制每个客户可以订购的产品。

create table permitted_products (
  cust_id integer not null references customers (cust_id),
  prod_id integer not null references products (prod_id),
  primary key (cust_id, prod_id)
);
insert into permitted_products values 
-- Cust 100 permitted to buy all three products
(100, 1), (100, 2), (100, 3),
-- Cust 200 permitted to buy only product 2.
(200, 2);

客户300没有允许的产品。

create table orders (
  ord_id integer primary key,
  cust_id integer not null references customers (cust_id)
);
insert into orders values
(1, 100), (2, 200), (3, 100);

表" order_line_items"是"魔术"发生。 {cust_id,prod_id}上的外键约束可防止在没有权限的情况下订购产品。

create table order_line_items (
  ord_id integer not null,
  line_item integer not null check (line_item > 0),
  cust_id integer not null,
  prod_id integer not null,
  foreign key (ord_id) references orders (ord_id),
  foreign key (cust_id, prod_id) references permitted_products (cust_id, prod_id),
  primary key (ord_id, line_item)
);

insert into order_line_items values
(1, 1, 100, 1), (1, 2, 100, 2), (1, 3, 100, 3);
insert into order_line_items values
(2, 1, 200, 2);
insert into order_line_items values
(3, 1, 100, 3);

您可以为客户300开始订购。 。

insert into orders values (4, 300);

。 。 。但是您无法插入任何订单项。

insert into order_line_items values (4, 1, 300, 1);
ERROR:  insert or update on table "order_line_items" violates foreign key constraint "order_line_items_cust_id_fkey"
DETAIL:  Key (cust_id, prod_id)=(300, 1) is not present in table "permitted_products".