主頁 > 知識庫 > MySQL表排序規(guī)則不同錯誤問題分析

MySQL表排序規(guī)則不同錯誤問題分析

熱門標簽:400電話申請什么好 電話機器人免費嗎 地圖標注圖標素材入駐 電銷機器人價格多少錢一臺 百度地圖標注地方備注 好搜地圖標注 怎么辦理400電話呢 怎么申請400電話申請 龍圖酒吧地圖標注

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

就是說兩個表的排序規(guī)則(COLLATION)不同,無法完成比較。COLLATION是用在排序,大小比較上,一個字符集有一個或多種COLLATION,并且以_ci(大小寫不敏感)、_cs(大小寫敏感)或_bin(二進制)結(jié)束。在做比較時,應(yīng)該確保兩個表的字符排序相同。一般建表的時候不指定,可以走默認的,全是默認的就沒什么問題了。

下面來模擬一下各種場景,表結(jié)構(gòu)如下(utf8默認排序規(guī)則為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)

查看表默認排序規(guī)則集

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)

查看列排序規(guī)則集

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.

如果使用下面這種方式修改字符集,你會發(fā)現(xiàn),只更改了表級的,沒有更改列級的。

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)

要僅僅改變一個表的默認字符集,應(yīng)使用此語句:

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)

可以發(fā)現(xiàn)列字符集沒有改變,并且只有新的列才會默認繼承表的字符集(utf8.utf8_general_ci)。

總結(jié)

以上就是本文關(guān)于MySQL表排序規(guī)則不同錯誤問題分析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:幾個比較重要的MySQL變量、MySQL 聲明變量及存儲過程分析、MySQL主庫binlog(master-log)與從庫relay-log關(guān)系代碼詳解、MySQL prepare原理詳解等,有什么問題可以隨時留言,互相交流,共同進步。

您可能感興趣的文章:
  • MySQL中Order By多字段排序規(guī)則代碼示例
  • Mysql中的排序規(guī)則utf8_unicode_ci、utf8_general_ci的區(qū)別總結(jié)

標簽:浙江 內(nèi)江 廣西 防疫工作 汕尾 撫順 溫州 固原

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MySQL表排序規(guī)則不同錯誤問題分析》,本文關(guān)鍵詞  MySQL,表,排序,規(guī)則,不同,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL表排序規(guī)則不同錯誤問題分析》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL表排序規(guī)則不同錯誤問題分析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章