重复密钥更新时的mysql插入导致id不连续

omvjsjqw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(320)

我使用以下sql创建了一个表:

CREATE TABLE import_xlsx (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
date DATETIME,
pair VARCHAR(10),
action VARCHAR(5),
volume DECIMAL(20,8),
order_price DECIMAL(20,8),
trading_price DECIMAL(20,8),
status VARCHAR(20),
CONSTRAINT table_unique UNIQUE (date, pair, action));

我有这个 INSERT :

INSERT INTO binance_xlsx (date, pair, action, volume, order_price, trading_price, status)
VALUES('2018-05-14 13:58:54', 'ETHUSDT', 'SELL', '0.1', '725.0', '724.95', 'Filled') 
ON DUPLICATE KEY UPDATE 
status = VALUES(status);

我故意插入了重复的行,数字id是自动递增的。但行数仍然是1。不应该只在插入新行时自动递增吗?
结果如下:

+----+---------------------+---------+--------+------------+--------------+---------------+--------+
| id | date                | pair    | action | volume     | order_price  | trading_price | status |
+----+---------------------+---------+--------+------------+--------------+---------------+--------+
|  1 | 2018-05-14 13:58:53 | ETHUSDT | BUY    | 0.09889000 | 725.00000000 |  724.95000000 | Filled |
| 11 | 2018-05-14 13:58:53 | ETHUSDT | SELL   | 0.10000000 | 725.00000000 |  724.95000000 | Filled |
| 13 | 2018-05-14 13:58:54 | ETHUSDT | SELL   | 0.10000000 | 725.00000000 |  724.95000000 | Filled |
+----+---------------------+---------+--------+------------+--------------+---------------+--------+

如何保持 id 按顺序自动递增?

f4t66c6m

f4t66c6m1#

那只是因为你的主键´s是自动递增的列,因此当您尝试插入时,不会得到重复的键并插入新行。这是您为 id 列:

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY

您应该更改它并将主键设置为 (date, pair, action) 价值观,所以它将工作

相关问题