
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to convert an MySQL database characterset and collation to UTF-8?
Firstly, we will check which MySQL version is currently being used with the help of version() function −
The query is as follows −
mysql> SELECT version();
The following is the output
+-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)
As you can see in the above output, version 8.0.12 is being used. Now, we can check the current character encoding using the following syntax −
SELECT CCSA.character_set_name FROM information_schema.`TABLES`T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name -> =T.table_collation AND T.table_schema = "yourDatabaseName" AND T.table_name = "yourTableName";
Apply the above query −
mysql> SELECT CCSA.character_set_name FROM information_schema.`TABLES`T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name -> =T.table_collation AND T.table_schema = "sample" AND T.table_name = "mytable";
The following is the output:
+--------------------+ | CHARACTER_SET_NAME | +--------------------+ | utf8mb4 | +--------------------+ 1 row in set (0.00 sec)
Here is the query by which we can set utf8 for a database −
mysql> ALTER DATABASE sample CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected, 1 warning (0.27 sec)
Let us check −
mysql> SELECT CCSA.character_set_name FROM information_schema.`TABLES`T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name -> =T.table_collation AND T.table_schema = "sample" AND T.table_name = "mytable";
The following is the output
+--------------------+ | CHARACTER_SET_NAME | +--------------------+ | utf8mb4 | +--------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to convert wrongly encoded data to UTF-8 in MySQL?
- Convert Unicode to UTF-8 in Java
- Convert UTF-8 to Unicode in Java
- Convert String to UTF-8 bytes in Java
- Convert ASCII TO UTF-8 Encoding in PHP?
- Convert the column to a case-sensitive collation in MySQL?
- MySQL query to discover current default database collation (via command line client)?
- Change MySQL default character set to UTF-8 in my.cnf?
- How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters in java?
- How to read and write unicode (UTF-8) files in Python?
- How to represent Unicode strings as UTF-8 encoded strings using Tensorflow and Python?
- UTF-8 Validation in C++
- How to Convert a Java 8 Stream to an Array?
- How can Tensorflow text be used to split the UTF-8 strings in Python?
- How to insert an image in to MySQL database using Java program?

Advertisements