MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

How to Update MySQL Collation for all tables in the schema

January 2, 2025 By Prasanna Manjunatha

As per MySQL docs, MySQL Collation is a set of rules for comparing characters in a character set. MySQL collation affect how Strings are sorted and compared with each other. While creating a database table, you will have multiple collation options to choose from. Your choice of collation while creating a database mainly depends on your user base and/or their expectations. You can read more about all collation’s & their differences from the MySQL website.

Also Read : MySQL Scheduler Overview with Examples

The default MySQL Collation and charset is latin1. But, you may have a reason or a requirement to change the default collation, lets say to utf8_general_cli. There are multiple options to achieve this in MySQL. Below are the 3 options to update the MySQL Collation.

1. Configure MySQL Collation at the sever level

MySQL provides an option to configure the default collation at the MySQL server level. You can choose to use this option if you want to use the same collate for all the applications accessing your MySQL instance. You can use the below configuration in the MySQL configuration file,

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

2. Specify the MySQL Collation while creating the Schema

If you have multiple schemas in your database and if you want to update the collate only for a specific schema, then you can specify the collate while creating the schema itself. The collate used while creating the schema becomes the default collate for all the tables created in that schema. Below is the syntax to specify the collate while creating the schema.

CREATE DATABASE testdb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

Also Read: How to configure JaCoCo for Code Coverage in Spring boot Applications

If you want to update the collate for an already existing schema, then you can consider exporting the complete schema & then importing it back with updated collation.

3. Use Alter statement on Tables

You can change the collate for a particulat table by using Alter statement as well. This approach would be useful if you want to update the collate for certain specific tables in the schema. Below is the syntax to update the collate for a particular table,

 'ALTER TABLE `mydb`.`mytable` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'

‘mydb’ & ‘mytable’ needs to be replaced with database and table names respectively.

With a bit of tweek you can also use this approach to update the collate for all the tables in a database as well. If you run the below command, it will give you a list of Alter statements for each of the tables in the database. You will have to replace ‘mydb’ in the last line with the relavant schema name.

SELECT CONCAT('ALTER TABLE `', tbl.`TABLE_SCHEMA`, '`.`', tbl.`TABLE_NAME`, '` CONVERT TO 
CHARACTER SET utf8 COLLATE utf8_general_ci;')
FROM `information_schema`.`TABLES` tbl WHERE tbl.`TABLE_SCHEMA` = 'mydb'

Now, all you have to do is to copy the output from the above query and execute the alter statements for each of the tables.

Do let me know if you need any help in updating your MySQL collation.

email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

  • How to Solve Maven invalid CEN header error

  • How to check whether a Collection is Null or Empty in Java

  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File

  • How to Generate MD5 Hash in Java

  • How to Check Free Disk Space in Java

  • HTTP Redirection Configuration in JBoss

  • How to Deploy Exploded WAR file in JBoss EAP Server

  • How to Update MySQL Collation for all tables in the schema

  • How to configure JaCoCo for Code Coverage in Spring boot Applications

  • Wildcard SSL Certificate Configuration in JBoss

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 CodeCoverage Code Coverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Spring boot Springboot SQL Tools Tutorial web service

All time Popular Posts

  • How to Solve Maven invalid CEN header error

  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File

  • How to check whether a Collection is Null or Empty in Java

  • How to configure JaCoCo for Code Coverage in Spring boot Applications

  • How to Generate MD5 Hash in Java

  • How to Check Free Disk Space in Java

  • How to Deploy Exploded WAR file in JBoss EAP Server

  • Automate Audit Fields in Spring Boot with MyBatis: A Generic Solution for CRUD Operations

  • How to Password Protect a PDF File using Java

  • Getting Started with Spring Boot: A Spring Boot Setup Tutorial

Copyright © 2025 MyJavaCode.com