Choosing Character Sets in MySQL: A Short Guide
Tags: mysql, performance, webdev, tutorial
Published: 2022-11-13 04:00:00

<!-- wp:paragraph -->
<p>In this blog, we will look a little closer into the character sets available in MySQL – we will tell you what they are, and how best to work with them.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What are Character Sets and Why Are They Important?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we dive deeper into character sets in MySQL, we should probably start by explaining a couple of the core concepts related to them. Essentially, a character set is a set of characters that are allowed to be used as part of a value of a column. While collations refer to rules that are used to compare given characters, character sets are sets of symbols and encodings.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Character sets are generally important because when combined with collations, they make a really powerful combination – for example, the big5 character set comes with the default collation of big5_chinese_ci that can make the usage of Chinese characters possible in MySQL and MariaDB, the latin1 character set comes with the latin1_swedish_ci collation that can be used to support characters relevant to the Swedish language, we can use a character set known as “sjis” to support Japanese characters (such character set comes with the collation of ujis_japanese_ci), etc.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How to Choose a Proper Character Set?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Contrary to a popular belief, choosing to use a proper character set in MySQL is rather simple. You can simply run a query like <code>SHOW CHARACTER SET \G;</code> and you should see something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>************************** 1. row ***************************Charset: big5Description: Big5 Traditional ChineseDefault collation: big5_chinese_ciMaxlen: 2*************************** 2. row ***************************Charset: dec8Description: DEC West EuropeanDefault collation: dec8_swedish_ciMaxlen: 1</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, there is another approach. Run a query like so:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>SELECT * FROM information_schema.CHARACTER_SETS ORDER BY CHARACTER_SET_NAME</code> and you will be able to observe all of the character sets ordered by their name from A to Z as well. Here is what you will see:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":496,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://breachdirectory.com/blog/wp-content/uploads/2022/08/image-4.png" alt="" class="wp-image-496"/><figcaption>The Character Sets Available in MySQL</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In this case, we think that it should be a little easier to choose character sets than collations in MySQL because you are already able to see that MySQL combines both character sets and collations and also gives you a short description of what a certain collation is (in other words, what kinds of languages it might be relevant to, etc.)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, in most cases, to choose a proper character set you would need to evaluate the requirements of your project upfront. First off, you would need to think about your database schemas – are they optimized? Once you have taken a deeper look into your database schemas, think about the data you are about to store – are you storing big data sets in a certain storage engine? What data does your data consist of? Are you storing usernames? geographical locations? names? surnames? If you are storing names or surnames, in what country does the people reside (that might be a decisive factor as well – remember, certain languages (for example, Swedish, Russian, etc.) have characters unique to them)? – make sure to consider all factors that might play a decisive role as far as languages are related, then issue a query like the one above and choose your character sets wisely. Of course, you can always change them if you don’t like something, but choosing something upfront is always better than changing something as you go, right?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, if you find that you don’t like the character sets you elected to use, you can always change them by issuing a query like so where <code>utf8mb4</code> is the name of any character set you elect to use:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>ALTER TABLE demo_table CONVERT TO CHARACTER SET utf8mb4;</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That’s it – once you know both how to choose character sets and how to convert your table to a given character set, you should be ready to get rolling on the high-performance road! If you find that the information we have provided is incomplete, though, feel free to refer to <a href="https://dev.mysql.com/doc/refman/8.0/en/">the MySQL documentation</a> or head back to <a href="https://arctype.com/blog">the BreachDirectory blog</a> – we cover a bunch of interesting topics related to information security, data breaches, development, and other things as well, so you will certainly find something of interest to you! Be well, and until next time.</p>
<!-- /wp:paragraph -->