MySQL唯一键报错的时候会将唯一键的所有健值打印出来:
##主键冲突: mysql> show create table t; +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t | CREATE TABLE `t` ( `id` int(11) NOT NULL, `a` int(11) NOT NULL, `d` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL, `b` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`,`a`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> insert into t value(5,1,2,3,'test'); ERROR 1062 (23000): Duplicate entry '5-1' for key 'PRIMARY' ##唯一索引冲突: mysql> show create table t; +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t | CREATE TABLE `t` ( `id` int(11) NOT NULL, `a` int(11) NOT NULL, `d` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL, `b` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `a` (`a`,`b`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> insert into t value(6,1,2,3,'test'); Query OK, 1 row affected (0.02 sec) mysql> insert into t value(7,1,2,3,'test'); ERROR 1062 (23000): Duplicate entry '1-test' for key 'a'