Search code examples
mysqlcharacter-set

What is the priority of mysql character set?


You can check the character set of show create table 'table', show create database 'database', \s with db, table, system.

What character set does this table use if the situation is as below?

If the client is utf8mb4 and the table is utf8mb4, there is no problem using it as utf8mb4. Can I ignore all other settings?

1.
mysql > show create database db_name;
+-----------+----------------------------------------------------------------------+
| Database  | Create Database                                                      |
+-----------+----------------------------------------------------------------------+
| db_name   | CREATE DATABASE `db_name` /*!40100 DEFAULT CHARACTER SET latin1 */   |
+-----------+----------------------------------------------------------------------+

2.
mysql > show create table tb_name;
CREATE TABLE `tb_name` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `var` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

3.
\s
Server characterset:    utf8
Db     characterset:    latin1
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4

Solution

  • Every column has its own character set and collation; the table, database, and server character sets are simply the defaults for newly created new columns, tables, and databases (respectively). But show create table will show the character set for any columns that differ from the table default, so if you don't see any, the table's character set is what its columns are using.