Setting Character Sets and Collations
Complete Setting Character Sets and Collations data type guide for MariaDB. Complete reference for syntax, valid values, storage requirements, and range.
The default character set is utf8mb4 and the default collation is utf8mb4_uca1400_ai_ci.
This may differ in some distros, see for example Differences in MariaDB in Debian.
The default character set is latin1 and the default collation is latin1_swedish_ci.
This may differ in some distros, see for example Differences in MariaDB in Debian.
In MariaDB 11.6, the default character set changed from latin1 to utf8mb4.
When upgrading to 11.6 or above from a previous release series, this can lead to behavior different from what you've been seeing in the old version.
See this section for details, including the impact on replicating to older MariaDB (or MySQL) replicas.
The character sets and the collations can be specified from the server right down to the column level, as well as for client-server connections. When changing a character set and not specifying a collation, the default collation for the new character set is always used.
Character sets and collations always cascade down, so a column without a specified collation will look for the table default, the table for the database, and the database for the server. It's therefore possible to have extremely fine-grained control over all the character sets and collations used in your data.
Default collations for each character set can be viewed with the SHOW COLLATION statement, for example, to find the default collation for the latin2 character set:
SHOW COLLATION LIKE 'latin2%';
+---------------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+---------------------+---------+----+---------+----------+---------+
| latin2_czech_cs | latin2 | 2 | | Yes | 4 |
| latin2_general_ci | latin2 | 9 | Yes | Yes | 1 |
| latin2_hungarian_ci | latin2 | 21 | | Yes | 1 |
| latin2_croatian_ci | latin2 | 27 | | Yes | 1 |
| latin2_bin | latin2 | 77 | | Yes | 1 |
+---------------------+---------+----+---------+----------+---------+Server Level
The character_set_server system variable can be used to change the default server character set. It can be set both on startup or dynamically, with the SET command:
Similarly, the collation_server variable is used for setting the default server collation.
Database Level
The CREATE DATABASE and ALTER DATABASE statements have optional character set and collation clauses. If these are left out, the server defaults are used.
To determine the default character set used by a database, use:
Alternatively, for the character set and collation:
It is also possible to specify only the collation, and, since each collation only applies to one character set, the associated character set will automatically be specified.
Although there are character_set_database and collation_database system variables which can be set dynamically, these are used for determining the character set and collation for the default database, and should only be set by the server.
Table Level
The CREATE TABLE and ALTER TABLE statements support optional character set and collation clauses, a MariaDB and MySQL extension to standard SQL.
If neither character set nor collation is provided, the database default will be used. If only the character set is provided, the default collation for that character set will be used . If only the collation is provided, the associated character set will be used. See Supported Character Sets and Collations.
If no collation is provided, the collation will be set to the default collation for that character set. See Supported Character Sets and Collations.
For VARCHAR or TEXT columns, CONVERT TO CHARACTER SET changes the data type if needed to ensure the new column is long enough to store as many characters as the original column.
For example, an ascii TEXT column requires a single byte per character, so the column can hold up to 65,535 characters. If the column is converted to utf8mb4, 4 bytes can be required for each character, so the column will be converted to MEDIUMTEXT to be able to hold the same number of characters.
CONVERT TO CHARACTER SET binary will convert CHAR, VARCHAR and TEXT columns to BINARY, VARBINARY and BLOB respectively, and from that point will no longer have a character set, or be affected by future CONVERT TO CHARACTER SET statements.
To avoid data type changes resulting from CONVERT TO CHARACTER SET, use MODIFY on the individual columns instead. For example:
Column Level
Character sets and collations can also be specified for columns that are character types CHAR, TEXT or VARCHAR. The CREATE TABLE and ALTER TABLE statements support optional character set and collation clauses for this purpose - unlike those at the table level, the column level definitions are standard SQL.
If neither collation nor character set is provided, the table default is used. If only the character set is specified, that character set's default collation is used, while if only the collation is specified, the associated character set is used.
When using ALTER TABLE to change a column's character set, you need to ensure the character sets are compatible with your data. MariaDB will map the data as best it can, but it's possible to lose data if care is not taken.
The SHOW CREATE TABLE statement or INFORMATION SCHEMA database can be used to determine column character sets and collations.
Filenames
The character_set_filesystem system variable has controlled interpretation of file names that are given as literal strings. This affects the following statements and functions:
Literals
By default, the character set and collation used for literals is determined by the character_set_connection and collation_connection system variables. However, they can also be specified explicitly:
The character set of string literals that do not have a character set introducer is determined by the character_set_connection system variable.
This query always returns the same character set name in both columns.:
character_set_client and character_set_connection are normally (e.g. during handshake, or after a SET NAMES query) are set to equal values. However, it's possible to set to different values.
Examples
Examples when setting @@character_set_client and @@character_set_connection to different values can be useful:
Example 1:
Suppose, we have a utf8 database with this table:
Now we connect to it using a client which uses the DOS character set (cp850 on a West European machine), and want to fetch all records that are equal to 'ö' according to the German phonebook rules.
It's possible with the following:
This will return:
It works as follows:
The client sends the query using
cp850.The server, when parsing the query, creates a utf8 string literal by converting 'ö' from
@@character_set_client(cp850) to@@character_set_connection(utf8).The server applies the collation
utf8_german2_cito this string literal.The server uses
utf8_german2_cifor comparison.
Note, if we rewrite the script like this:
We get an error:
Reason:
In step #2, the literal is not converted to
utf8any more and is created usingcp850.In step #3, the server fails to apply
utf8_german2_cito ancp850string literal.
Example 2:
Suppose we have a utf8 database and use "mysql.exe" from a West European machine again.
We can do this:
It creates a table with a column of the type VARCHAR(1) CHARACTER SET utf8.
Note, if we rewrite the query like this:
It creates a table with a column of the type VARCHAR(1) CHARACTER SET cp850, which is probably not a good idea.
N
Also, N or n can be used as prefix to convert a literal into the National Character set (which in MariaDB is always utf8).
For example:
Stored Programs and Views
The literals which occur in stored programs and views, by default, use the character set and collation which was specified by the character_set_connection and collation_connection system variables when the stored program was created. These values can be seen using the SHOW CREATE statements. To change the character sets used for literals in an existing stored program, it is necessary to drop and recreate the stored program.
For stored routines parameters and return values, a character set and a collation can be specified via the CHARACTER SET and COLLATE clauses.
The following example shows that the character set and collation are determined at the time of creation:
The following example shows how to specify a function parameters character set and collation:
Changing Default Collation
It is possible to change the default collation associated with a particular character set. The character_set_collations system variable accepts a comma-delimited list of character sets and new default collations, for example:
It is not possible to change the default collation associated with a particular character set.
The new variable takes effect in all cases where a character set is explicitly or implicitly specified without an explicit COLLATE clause, including but not limited to:
Column collation
Table collation
Database collation
CHAR(exprUSINGcsname)CONVERT(exprUSINGcsname)CAST(exprAS CHAR CHARACTER SETcsname)'' - character string literal
_utf8mb3'text'- a character string literal with an introducer_utf8mb3 X'61'- a character string literal with an introducer with hex notation_utf8mb3 0x61- a character string literal with an introducer with hex hybrid notation@@collation_connectionafter aSET NAMESstatement withoutCOLLATE
Default Character Set and Collation Changes
The default character set and collation changed in MariaDB 11.8.
The default character set has changed from latin1 to utf8mb4, and the default collation has changed from latin1_swedish_ci to utf8mb4_uca1400_ai_ci. This update improves global compatibility and supports modern data requirements, such as emojis.
Why the Defaults Changed
The shift to utf8mb4 and UCA-based collations provides several benefits:
Global Compatibility: The new defaults support users worldwide without requiring additional configuration, whereas the previous
latin1defaults were primarily suited for West European languages.Supplementary Character Support: You can now store supplementary characters, such as emojis, which were not supported by
latin1.Accurate Sorting and Comparison: The
utf8mb4_uca1400_ai_cicollation correctly handles supplementary characters and supports expansions and contractions from the Default Unicode Collation Element Table (DUCET). For example, the German character "ß" is correctly compared as equal to "ss".
Important Considerations
Before upgrading, please be aware of the following technical implications:
The storage overhead for
CHAR(N)indeed significantly increases. The server reservedNbytes for aCHAR(N) CHARACTER SET latin1column in every record. Now it must reserveN*4bytes for aCHAR(N) CHARACTER SET utf8mb4column, and fill the unused bytes with trailing spacing.The storage overhead for a
VARCHAR(N)is not really palpable because the server only stores actual strings without padding. West European languages mostly use basic ASCII letters, only rarely accented letters. For example, for German, the letter use statistics is here: https://www.sttmedia.com/characterfrequency-german. After switching from latin1 to utf8mb4, only accented letters need more storage, but since they're making up only for a small portion, the overall growth is insignificant.The previous two points indicate that it might be worthwhile to consider switching from
CHARtoVARCHARcolumns.Replication Impact: Because
utf8mb4_uca1400_ai_ciwas not available in earlier versions, replication from MariaDB 11.8 to MariaDB 10.6 will fail unless the server is configured to use the old defaults.Storage Overhead: Using
utf8mb4may increase storage requirements forCHARandVARCHARcolumns, particularly for data containing non-ASCII characters. This is particularly true when table columns containlatin1code points outside of the ASCII range.Performance: Some comparison operations may be slower with the new collation compared to the fixed-width
latin1_swedish_cicollation.Application Compatibility: Some applications that rely on
latin1behavior may require updates to function correctly with the new defaults. Problems can occur because alatin1column can always compare to a binary string, while autf8mb4column cannot (at least not always), because not every binary string is a well-formedutf8mb4string. A workaround is to cast theutf8mb4column toBINARYbefore doing a comparison.
Restoring Old Defaults
If you need to maintain compatibility with MariaDB 10.6 replicas or require the specific performance characteristics of the previous defaults, you can configure the server to use the old settings.
To return to the latin1 defaults, add the following lines to your my.cnf configuration file:
Note: These settings make data files compatible with older versions like MariaDB 10.6, which lack the newer UCA collation support.
Example: Changing the Default Character Set To UTF-8
To change the default character set from latin1 to UTF-8, the following settings should be specified in the my.cnf configuration file.
Note that the default-character-set option is a client option, not a server option.
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

