How to change the default charset of a MySQL table?

MySQLMySQLi Database

To change the default charset of a MySQL table, you can use the below syntax. The syntax is as follows −

alter table yourTableName convert to character set yourCharsetName;

Let us create a table and apply the above syntax to change the default charset. The query to create a table −

mysql> create table CharsetDemo
   -> (
   -> Id int,
   -> Name varchar(200),
   -> Age int
   -> );
Query OK, 0 rows affected (0.73 sec)

Now you can change the charset of a table. The following is the query to set the charset, which in turn will change the default charset.

mysql> alter table CharsetDemo convert to character set latin7 ;
Query OK, 0 rows affected (1.59 sec)
Records: 0 Duplicates: 0 Warnings: 0

Now you can check that the charset has been changed or not with the help of SHOW CREATE command.

The query is as follows −

mysql> show create table CharsetDemo;

Here is the output −

+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                      |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CharsetDemo | CREATE TABLE `charsetdemo` (`Id` int(11) DEFAULT NULL,`Name` varchar(200) DEFAULT NULL,`Age` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin7           |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Look at the above output, the charset have been changed to latin7.

raja
Published on 24-Dec-2018 07:14:21
Advertisements