How to change collation to utf8_bin in a single line?

MySQLMySQLi Database

You need to use ALTER command to change collation to utf8_bin. The syntax is as follows:

ALTER TABLE yourTableName COLLATE utf8_general_ci;

To understand the above syntax, let us create a table. The query to create a table is as follows:

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)

Check the DDL of the table. The syntax is as follows:

SHOW CREATE TABLE yourTableName;

Let us now check the DDL of our table:

mysql> show create table CollateDemo;

The following is the output:

+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 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)

Now you can change the collation using ALTER command. The query is as follows:

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

Now let us check the DDL of the table:

mysql> show create table CollateDemo;

The following is the output:

+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 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)
raja
Published on 01-Feb-2019 12:44:24
Advertisements