How to change collation of database, table, column?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Collation in database systems is a set of rules that determines how data is sorted and compared. It is particularly crucial in defining character set sorting rules for textual data. These rules affect aspects such as letter case sensitivity and accent marks. For databases using Unicode (such as UTF-8), understanding collation is essential to ensure that your data retrieval and storage behave as expected. This article will guide you through changing the collation of a database, a table, or a column in MySQL, providing both theoretical background and practical examples.
What is Collation?
Collation is a collection of rules that defines how string comparison should be performed. It specifies three main aspects:
- Character Set: A set of symbols and encodings.
- Comparison rules for sorting and matching: Including case sensitivity and accent sensitivity.
- Sorting order: Defines alphabetical order, which can vary between languages.
MySQL, a popular relational database management system, supports multiple collations. When setting a collation, you also implicitly specify a character set. For example, utf8_general_ci is a collation that uses the utf8 character set.
Why Change Collation?
There are several reasons you might need to change the collation of your database:
- Performance: Certain collations may increase the speed of queries, especially when dealing with large datasets or complex comparisons.
- Internationalization: Different collations support different language-specific sorting rules. For example,
utf8_unicode_ciprovides more accurate sorting for international text thanutf8_general_ci. - Consistency: When migrating data between databases, ensuring consistent collation can help prevent errors.
Changing Collation
Database Level
Changing the collation of a database can be done using the ALTER DATABASE statement. It changes the default collation for tables created afterwards but does not alter existing tables or their columns.
Key Points:
- Ensure backups are done before making changes.
- Changing the default does not affect existing tables.
Table Level
To change the collation of all string columns within a table:
Key Points:
- Useful for altering numerous columns simultaneously.
- Can temporarily increase disk usage.
Column Level
To change the collation of a specific column:
Key Points:
- Specific for altering individual columns.
- You need to specify the data type and its length again.
Additional Considerations
Testing Changes
Before fully committing to a collation change, it is wise to test how your application handles the changes, particularly if you're dealing with a multilingual dataset.
Backup Strategy
- Always back up data before making significant changes.
- Test backups to ensure data integrity post-migration.
Collation Comparison Table
| Character Set | Collation Name | Case Sensitivity | Accent Sensitivity | Use Case |
| utf8mb4 | utf8mb4_general_ci | Case Insensitive | Accent Insensitive | General-use, benefits in search speed |
| utf8mb4 | utf8mb4_unicode_ci | Case Insensitive | Accent Sensitive | More accurate sorting for international characters |
| utf8mb4 | utf8mb4_bin | Case Sensitive | Accent Sensitive | Binary comparison, useful for exact matches |
Conclusion
Understanding and altering collation in MySQL is crucial when needing different sorting, matching, or comparison rules for your data. While changing collation is a straightforward task, its implications can be significant. Always ensure you thoroughly test and back up your data before proceeding with collation changes.
By leveraging collation settings, you can optimize database performance, enhance compatibility, and maintain the integrity of multilingual text data, ensuring a robust database application.
Related reading
- How to change Column size of varchar type in mySQL?
- How to change connectionsPerHost and threadsAllowedToBlockForConnectionMultiplier configuration in mongdb through mongo shell?
- How to change CQL version?
- How to change max_allowed_packet size
- How to change MySQL column definition?
- How to change MySQL data directory?
- How to change the CHARACTER SET and COLLATION throughout a database?
- How to change the default charset of a MySQL table?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.