Skip to content

Instantly share code, notes, and snippets.

@jack3898
Created July 28, 2021 08:37
Show Gist options
  • Save jack3898/96c1157b47429f2b854cb501b06eb71f to your computer and use it in GitHub Desktop.
Save jack3898/96c1157b47429f2b854cb501b06eb71f to your computer and use it in GitHub Desktop.
A couple of queries that let you convert an entire database into a new collation.

Converting database collations for large databases (MySQL/MariaDB)

Make sure Barracuda is enabled and the default character set globally is set to your desired character set in my.cnf.

Query 1

ALTER DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci Of course, feel free to change the collations to what best suits you.

Then, get all of the queries to update the database tables and columns replacing the values that suit your situation:

Query 2

The below query will make no changes to the database. It will query the schema and construct a list of queries for you to copy and paste that convert the collation.

SELECT CONCAT('ALTER TABLE `', table_name, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C WHERE C.collation_name = T.table_collation AND T.table_schema = 'database_name' AND ( C.CHARACTER_SET_NAME != 'utf8mb4' OR C.COLLATION_NAME != 'utf8mb4_unicode_ci' );

Using PHPMyAdmin?

If something like PHPMyAdmin is available, you can:

  1. Export a CSV
  2. Remove the quotation marks at the beginning and end of each query
  3. Remove the commas at the end of each query
  4. Copy and paste the whole file into PHPMyAdmin to run all the queries.

If you're using a different tool that lets you export as CSV then you can use that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment