如何为每个客户 ID 提供单独的订单 ID?

发布于 2025-01-04 08:46:42 字数 739 浏览 1 评论 0原文

想象一个数据库设置,其中 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
);

imagine a database setup, where x customers can make y orders.
normally all customers would share one set of auto incremented order IDs.

  • the first order of customer A has order id #1
  • first order of customer B has order id #2.

i'd like to have individual sets of order IDs for each customer. it is important, that customers cannot see based on the id how many other customers have made orders.

  • first order of customer A has order id #1
  • second order of customer A has order id #2
  • first order of customer B has order id #1

right now, the only way i can see would be to manually select the max value for the customer's order number, add +1 and then insert it manually.

select max(customer_order_id) from orders where customer_id = X

how can i create this approach in a sense of data integrity and normalization? like having some kind of auto increment for the individual order ids of the customer?

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
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2025-01-11 08:46:42

只需将订单号设为客户编号,后跟序列号即可。因此,如果我是客户 103985,我的第一个订单是 103985-0001,我的第二个订单是 103985-0002。标准化数据——在订单中,分别存储客户编号和序列号。将它们组合起来进行显示。

Just make the order number the customer number followed by a sequence number. So if I'm customer 103985, my first order is 103985-0001, my second order is 103985-0002. Normalize the data -- in an order, store the customer number and the sequence number separately. Combine them for display.

您的好友蓝忘机已上羡 2025-01-11 08:46:42

听起来您想要一个基本的关系结构或只是一个可供选择的 slug 字段。

因此,您的订单表可以有一个对客户透明的自动增量 ID。根据您计划如何提供数据,您可以轻松使用 slug 系统来组合用户数据来查找正确的记录。

例如,基于 url 的请求 - somesite.com/orders/{user_id}/1 将选择其中用户 id 为登录用户,订单 slug 为 1。 {user_id} 可以存在或省略,具体取决于您的方式选择授权查看该页面。因此,管理员可以通过获取变量进行查看,非管理员必须针对此变量进行验证...但这可能不是重点。

或者,您可以使用 varchar 来处理订单 id 并组成一个完全无法破译的字符串。根据您网站的规模,这可能不是问题。

您唯一关心的是用户 B 的订单 id 为 100,您不想让他们知道之前还有 99 个其他订单吗?如果是这样,这里的任何答案都适合您。

It sounds like you want a basic relational structure or simply a slug field to select on.

So your orders table could have an auto increment id that is transparent to the customer. Depending on how you plan on serving your data, you could easily use a slug system that combines the userdata to find the correct record.

So for example, a request that was url based - somesite.com/orders/{user_id}/1 would select where user id is the logged in user and order slug is 1. {user_id} could be present or omitted depending on how you choose to authorize viewing the page. So admins can view by get vars and non admins have to validate against this var... but that may be beside the point.

Alternatively, you can use a varchar to handle order id and compose a completely undecipherable string. Depending on the scale of your site this may not be a problem.

Is your only concern is that user B has an order id of 100, you do not want them to know there were 99 other orders before? If so, any of the answers here would work for you.

怼怹恏 2025-01-11 08:46:42

与 MyISAM 相比,使用 InnoDB 有几个优点,但如果您使用 MyISAM 引擎,则可以拥有自动生成的复合主键。

来自 dev.mysql.com:示例自动增量。 html

对于 MyISAM 和 BDB 表,您可以在多个表中的辅助列上指定AUTO_INCRMENT -列索引。在本例中,AUTO_INCRMENT 列的生成值计算公式为 MAX(auto_increment_column) + 1 WHERE prefix=given-prefix。当您想要将数据放入有序组时,这非常有用。

在你的情况下,它会是这样的:

CREATE TABLE customers
(
  id int auto_increment PRIMARY KEY, 
  name varchar(255)
);

CREATE TABLE orders
(
  customer_id int,
  customer_order_id int auto_increment,  
  ...
  PRIMARY KEY (customer_id, customer_order_id)
);

如果你想要与 InnoDB 相同的功能,你将不得不使用触发器。

There are several advantages of using InnoDB over MyISAM but if you are using the MyISAM engine, you can have an auto generated compound Primary Key.

From dev.mysql.com: example-auto-increment.html:

For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

In your case, it would be something like this:

CREATE TABLE customers
(
  id int auto_increment PRIMARY KEY, 
  name varchar(255)
);

CREATE TABLE orders
(
  customer_id int,
  customer_order_id int auto_increment,  
  ...
  PRIMARY KEY (customer_id, customer_order_id)
);

If you want the same functionality with InnoDB, you'll have to use triggers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文