我正在尝试向列添加唯一键,并且出现
的自解释错误MariaDB [dnd]> ALTER TABLE ability
-> ADD UNIQUE INDEX UK_ability_shortname (shortname);
ERROR 1062 (23000): Duplicate entry '' for key 'UK_ability_shortname'
但问题是我看不到任何重复
MariaDB [INFORMATION_SCHEMA]> select * from INNODB_SYS_INDEXES WHERE NAME LIKE 'UK_%';
+----------+---------------------------------+----------+------+----------+---------+-------+
| INDEX_ID | NAME | TABLE_ID | TYPE | N_FIELDS | PAGE_NO | SPACE |
+----------+---------------------------------+----------+------+----------+---------+-------+
| 1733 | UK_food_food | 563 | 2 | 1 | 4 | 558 |
| 1737 | UK_type_type | 565 | 2 | 1 | 4 | 560 |
| 2077 | UK_invite_status_status | 763 | 2 | 1 | 4 | 752 |
| 2586 | UK_tool_class_name | 1093 | 2 | 1 | 4 | 1082 |
| 2591 | UK_food_drink_lodging | 1097 | 2 | 2 | 4 | 1086 |
| 2595 | UK_lifestyle_expenses_lifestyle | 1100 | 2 | 1 | 4 | 1089 |
| 2691 | UK_weapon_name | 1173 | 2 | 1 | 4 | 1162 |
| 2692 | UK_weapon_property_name | 1174 | 2 | 1 | 4 | 1163 |
| 2696 | UK_ability_name | 1183 | 2 | 1 | 4 | 1172 |
+----------+---------------------------------+----------+------+----------+---------+-------+
9 rows in set (0.00 sec)
还有什么想法可以检查一下,为什么数据库认为名称存在冲突,或者我是否超出某些索引名称限制?
我想要改变的表定义。
CREATE TABLE `ability` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`shortname` char(3) NOT NULL,
`description` text,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_ability_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Page 175'
使用:服务器版本:10.0.15-MariaDB MariaDB服务器
如果我能提供任何其他信息,请告诉我。
答案 0 :(得分:3)
错误消息表示至少有两行具有相同的值shortname
(''
:空字符串)。因此,MariaDB无法在该列上创建唯一索引。使用此查询在列中搜索任何(其他)重复值:
SELECT
shortname,
GROUP_CONCAT(id)
FROM
ability
GROUP BY
shortname
HAVING
COUNT(0) > 1