如何在一行内将排序规则更改为 utf8_bin?

mysqlmysqli database

您需要使用 ALTER 命令将排序规则更改为 utf8_bin。语法如下:

ALTER TABLE yourTableName COLLATE utf8_general_ci;

为了理解上述语法,让我们创建一个表。创建表的查询如下:

mysql> create table CollateDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Age int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.98 sec)

检查表的DDL。语法如下:

SHOW CREATE TABLE yourTableName;

Let us now check the DDL of our table:

mysql> show create table CollateDemo;

输出结果如下:

+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                                               |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CollateDemo | CREATE TABLE `collatedemo` (`Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(20) DEFAULT NULL, `Age` int(11) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci   |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

现在您可以使用 ALTER 命令更改排序规则。查询如下:

mysql> alter table CollateDemo collate utf8_general_ci;
Query OK, 0 rows affected (0.39 sec)
Records: 0 Duplicates: 0 Warnings: 0

现在让我们检查表的 DDL:

mysql> show create table CollateDemo;

输出结果如下:

+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                                  |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CollateDemo | CREATE TABLE `collatedemo` (`Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, `Age` int(11) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8       |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

相关文章