大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MySQL表排序规则不同报错是什么问题导致的?

MySQL表排序规则不同报错是什么问题导致的?很多人都不太了解,今天小编为了让大家更加了解MySQL表排序规则,所以给大家总结了以下内容,一起往下看吧。

成都创新互联从2013年创立,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元四川做网站,已为上家服务,为四川各地企业和个人服务,联系电话:028-86922220

MySQL多表join时报错如下:[Err]1267 – Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=

就是说两个表的排序规则(COLLATION)不同,无法完成比较。COLLATION是用在排序,大小比较上,一个字符集有一个或多种COLLATION,并且以_ci(大小写不敏感)、_cs(大小写敏感)或_bin(二进制)结束。在做比较时,应该确保两个表的字符排序相同。一般建表的时候不指定,可以走默认的,全是默认的就没什么问题了。本文主要介绍了MySQL表排序规则不同错误问题分析,希望能帮助到大家。

下面来模拟一下各种场景,表结构如下(utf8默认排序规则为utf8_general_ci):

mysql> show create table test.cs\G
*************************** 1. row ***************************
    Table: cs
Create Table: CREATE TABLE `cs` (
 `id` int(11) DEFAULT NULL,
 `name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

查看表默认排序规则集

mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test     | cs     | utf8_general_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)

查看列排序规则集

mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test     | cs     | id     | NULL      |
| test     | cs     | name    | utf8_general_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)

从utf8升级为utf8mb4是不支持online ddl的,如下:

mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8mb4,ALGORITHM=INPLACE,LOCK=NONE;
ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.

从utf8.utf8_general_ci变更为utf8.utf8_unicode_ci是不支持online ddl的,如下:

mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8 collate utf8_unicode_ci,ALGORITHM=INPLACE,LOCK=NONE;
ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.

如果使用下面这种方式修改字符集,你会发现,只更改了表级的,没有更改列级的。

mysql> ALTER TABLE cs CHARACTER SET utf8 collate utf8_unicode_ci;      
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
 
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';      
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test     | cs     | utf8_unicode_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)
 
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test     | cs     | id     | NULL      |
| test     | cs     | name    | utf8_general_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)

所以真正改字符集的时候别忘了加上CONVERT TO,如下:

mysql> ALTER TABLE cs CONVERT TO CHARACTER SET utf8 collate utf8_unicode_ci;
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
 
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test     | cs     | id     | NULL      |
| test     | cs     | name    | utf8_unicode_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)

要仅仅改变一个表的默认字符集,应使用此语句:

mysql> ALTER TABLE cs default CHARACTER SET utf8 collate utf8_general_ci,ALGORITHM=INPLACE,LOCK=NONE;         
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
 
mysql> select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.tables where table_name='cs';      
+--------------+------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_COLLATION |
+--------------+------------+-----------------+
| test     | cs     | utf8_general_ci |
+--------------+------------+-----------------+
1 row in set (0.00 sec)
 
mysql> select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from information_schema.COLUMNS where TABLE_NAME='cs';  
+--------------+------------+-------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLLATION_NAME |
+--------------+------------+-------------+-----------------+
| test     | cs     | id     | NULL      |
| test     | cs     | name    | utf8_unicode_ci |
+--------------+------------+-------------+-----------------+
2 rows in set (0.00 sec)

可以发现列字符集没有改变,并且只有新的列才会默认继承表的字符集(utf8.utf8_general_ci)。

以上就是MySQL表排序规则不同报错是什么问题导致的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注创新互联行业资讯频道哦!

创新互联在今年春节期间,新上线了三款“云数据库”的产品和服务 :云数据库MySQL、云数据库Redis以及云数据库Memcached。其中,云数据库MySQL是一种即开即用、稳定可靠、可弹性伸缩的“在线数据库服务”,具有多重安全防护措施和完善的性能监控体系,并提供专业的数据库备份、恢复及优化方案,使企业与用户能够更加专注于应用开发和业务发展。


本文标题:MySQL表排序规则不同报错是什么问题导致的?
转载来源:http://dzwzjz.com/article/jhpoji.html
在线咨询
服务热线
服务热线:028-86922220
TOP