【问题标题】:MySQL utf-8 not recognising special characters [duplicate]MySQL utf-8无法识别特殊字符[重复]
【发布时间】:2019-12-02 06:41:19
【问题描述】:

有人知道为什么会失败吗?

我创建了一个带有 char3 主键的 utf-8 表,当使用 MySQL Workbench 向其中插入记录时,它无法区分 A 和 Ä

我们正在使用 MySQL 5.1.73 和 Workbench 6.3.10

CREATE TABLE `test` (
  `citycode` char(3) NOT NULL,
    PRIMARY KEY (`citycode`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `test` (`citycode`) VALUES ('JLA');
INSERT INTO `test` (`citycode`) VALUES ('JLÄ');

Operation failed: There was an error while applying the SQL script to the database.
Executing:
INSERT INTO `trains4_copy`.`test` (`citycode`) VALUES ('JLA');
INSERT INTO `trains4_copy`.`test` (`citycode`) VALUES ('JLÄ');

ERROR 1062: 1062: Duplicate entry 'JLÄ' for key 'PRIMARY'
SQL Statement:
INSERT INTO `trains4_copy`.`test` (`citycode`) VALUES ('JLÄ')

【问题讨论】:

  • 您的字符集可能是 utf8,但您为列 citycode 设置的排序规则是什么?
  • 如果你想要Ä=Æ,那么使用utf8_german2_ci。 utf8_slovak_ci 将Ä 视为在“az”之后排序的字母;也许你想要那个?

标签: mysql utf-8 mysql-workbench


【解决方案1】:

参考这个link,将这些类型的字符集存储为二进制。

CREATE TABLE `test` (
  `citycode` char(3) NOT NULL,
    PRIMARY KEY (`citycode`)
) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_bin;

INSERT INTO `test` (`citycode`) VALUES ('JLA');
INSERT INTO `test` (`citycode`) VALUES ('JLÄ');

试试这个dbfiddle

【讨论】:

  • 谢谢你。这是整理。我使用的 utf8_unicode_ci 不起作用。当我将其更改为 utf8_bin 时,它起作用了。
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 2018-02-10
相关资源
最近更新 更多