setting database charset mysql code example

Example 1: mysql how to change default charset

mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_unicode_ci |
| collation_server     | utf8mb4_unicode_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)

Example 2: database collation mysql

The main difference is sorting accuracy (when comparing characters in the 
language) and performance. The only special one is utf8_bin which is for 
comparing characters in binary format.

utf8_general_ci is somewhat faster than utf8_unicode_ci, but less accurate 
(for sorting). The specific language utf8 encoding (such as utf8_swedish_ci) 
contain additional language rules that make them the most accurate to sort for 
those languages. Most of the time I use utf8_unicode_ci (I prefer accuracy to 
small performance improvements), unless I have a good reason to prefer a 
specific language.

You can read more on specific unicode character sets on the MySQL manual - 
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html

Tags:

Sql Example