没有自动增量的Mysql主ID?

时间:2015-03-10 10:38:44

标签: mysql sql database

我使用c#和mysql创建项目来创建POS程序,当PC离线时可以使用PC程序,它将连接到远程数据库服务器。

但是我发现我的订单ID在许多计算机上都使用了这个问题。

PC1 : 100001 100002 ...
PC2 : 110001 110002 ...

所以我决定在没有自动增量的情况下制作我的主订单ID。 问题是我能做到吗? 它看起来像这样

order_ID
100001
120001
100002
110001
110002
100003

如果它看起来像这样,我可以用它来搜索功能吗?或(索引)

因为它不是相同的数字我有PC客户端的2digis号码。

100001
10 is prefix for PC and 0001 is order number

我在DB Server上的完整表格将是这样的

id(not use auto increment but Primary key)
100001
130001
100002

在我的离线电脑上就像这样

10 is PC1
id        prefix     number(this will use auto increment and Primary key)
100001      10        0001
100002      10        0002

13 is PC2
id        prefix     number(this will use auto increment and Primary key)
130001      13        0001

因此,当我想搜索订单时,我将“从ordertable中选择id = 130001”

那对吗? 如果那是对的,是否表现不佳?

谢谢。

1 个答案:

答案 0 :(得分:1)

您的解决方案很好,但每台PC限制为9999条记录。您的主键不必是自动增量整数。主键的唯一要求是它是唯一的。

然而,假设有InnoDB,如果有很多插入,你仍然可以考虑将代理自动增量整数作为主键,并简单地添加一个带有唯一索引的附加列。 “ID”。使用InnoDB,主键是聚簇索引。当按升序完成时,插入聚簇索引会更快(并且导致并发问题更少)。

相关问题