【发布时间】:2012-02-23 04:51:18
【问题描述】:
想象一个数据库设置,其中 x 个客户可以下 y 个订单。 通常所有客户都会共享一组自动递增的订单 ID。
- 客户 A 的第一个订单的订单 ID 为 #1
- 客户 B 的第一个订单的订单 ID 为 #2。
我想为每个客户分别设置一组订单 ID。重要的是,客户无法根据 id 看到有多少其他客户下了订单。
- 客户 A 的第一个订单的订单 ID 为 #1
- 客户 A 的第二个订单的订单 ID 为 #2
- 客户 B 的第一个订单的订单 ID 为 #1
现在,我能看到的唯一方法是手动选择客户订单号的最大值,添加 +1,然后手动插入。
select max(customer_order_id) from orders where customer_id = X
我怎样才能在数据完整性和规范化的意义上创建这种方法?喜欢为客户的各个订单 ID 设置某种自动增量?
create table customers
(
id int auto_increment primary key,
name varchar(255)
);
create table orders
(
id int auto_increment primary key,
customer_id int
customer_order_id int
foo varchar
bar varchar
);
【问题讨论】:
标签: mysql data-modeling