mysql回滚后重复唯一键错误
我有一个这样的表,
mysql> describe seudonimos;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id_seudonimo | int(11) unsigned | NO | PRI | NULL | auto_increment |
| seudonimo | varchar(45) | NO | UNI | NULL | |
+--------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
假设它是空的,所以自动增量为0。例如:
SET AUTOCOMMIT=0;
START TRANSACTION;
INSERT INTO seudonimos (seudonimo) VALUES ('Agatha Christie');
ROLLBACK;
SET AUTOCOMMIT=1;
据我所知,回滚不会影响自动增量。因此,如果我插入一个新值,自动增量将为 2 而不是 1。但是,如果我尝试再次插入“Agatha Christie”,则会出现以下问题:
INSERT INTO seudonimos (seudonimo) VALUES ('Agatha Christie');
#1062 - Duplicate entry 'Agatha Christie' for key 'seudonimo'
这不是我所期望的。我预料到了这一点:
+--------------+--------------------+
| id_seudonimo | seudonimo |
+--------------+--------------------+
| 2 | Agatha Christie |
+--------------+--------------------+
怎么了?
I have a table like this
mysql> describe seudonimos;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id_seudonimo | int(11) unsigned | NO | PRI | NULL | auto_increment |
| seudonimo | varchar(45) | NO | UNI | NULL | |
+--------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
Let's assume that it is empty so autoincrement is 0. For example:
SET AUTOCOMMIT=0;
START TRANSACTION;
INSERT INTO seudonimos (seudonimo) VALUES ('Agatha Christie');
ROLLBACK;
SET AUTOCOMMIT=1;
As far as I konw, the rollback does not affect the autoincrement. So if I insert a new value the autoincrement will be 2 instead of 1. But if I try to insert 'Agatha Christie' again, I have the following problem:
INSERT INTO seudonimos (seudonimo) VALUES ('Agatha Christie');
#1062 - Duplicate entry 'Agatha Christie' for key 'seudonimo'
That is not what I expected. I expected this:
+--------------+--------------------+
| id_seudonimo | seudonimo |
+--------------+--------------------+
| 2 | Agatha Christie |
+--------------+--------------------+
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能使用 MyISAM 引擎...运行
show create seudonimos;
并查看。You probably use MyISAM engine... Run
show create seudonimos;
and see.