关于sql:如何查看MySQL数据库/表/列的字符集是什么?

How do I see what character set a MySQL database / table / column is?

什么是(默认)字符集:

  • MySQL数据库

  • mysql表

  • MySQL列


我就是这么做的-

对于架构:

1
2
SELECT default_character_set_name FROM information_schema.SCHEMATA
WHERE schema_name ="schemaname";

对于表格:

1
2
3
4
5
SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
       information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
  AND T.table_schema ="schemaname"
  AND T.table_name ="tablename";

对于列:

1
2
3
4
SELECT character_set_name FROM information_schema.`COLUMNS`
WHERE table_schema ="schemaname"
  AND TABLE_NAME ="tablename"
  AND column_name ="columnname";


对于列:

1
SHOW FULL COLUMNS FROM TABLE_NAME;


对于数据库:

1
2
3
4
USE your_database_name;
SHOW VARIABLES LIKE"character_set_database";
-- or:
-- show variables like"collation_database";

参见本页。查看mysql手册


对于服务器上的所有数据库:

1
mysql> SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;

输出:

1
2
3
4
5
6
7
+----------------------------+---------+--------------------+
| DATABASE                   | charset | collation          |
+----------------------------+---------+--------------------+
| information_schema         | utf8    | utf8_general_ci    |
| my_database                | latin1  | latin1_swedish_ci  |
...
+----------------------------+---------+--------------------+

对于单个数据库:

1
2
mysql> USE my_database;
mysql> SHOW VARIABLES LIKE"character_set_database";

输出:

1
2
3
4
5
    +----------------------------+---------+
    | Variable_name              |  VALUE  |
    +----------------------------+---------+
    | character_set_database     |  latin1 |
    +----------------------------+---------+

正在获取表的排序规则:

1
2
mysql> USE my_database;
mysql> SHOW TABLE STATUS WHERE NAME LIKE 'my_tablename';

或-将为创建表输出完整的SQL:

埃多克斯1〔3〕

正在获取列的排序规则:

1
mysql> SHOW FULL COLUMNS FROM my_tablename;

输出:

1
2
3
4
5
6
7
+---------+--------------+--------------------+ ....
| FIELD   | TYPE         | collation          |
+---------+--------------+--------------------+ ....
| id      | INT(10)      | (NULL)             |
| KEY     | VARCHAR(255) | latin1_swedish_ci  |
| VALUE   | VARCHAR(255) | latin1_swedish_ci  |
+---------+--------------+--------------------+ ....


对于表格:

SHOW TABLE STATUS将列出所有表。

筛选使用:

1
SHOW TABLE STATUS WHERE name LIKE 'table_123';


对于数据库:

只需使用以下命令:

1
2
3
4
USE db_name;
SELECT @@character_set_database;
-- or:
-- SELECT @@collation_database;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT TABLE_SCHEMA,
       TABLE_NAME,
       CCSA.CHARACTER_SET_NAME AS DEFAULT_CHAR_SET,
       COLUMN_NAME,
       COLUMN_TYPE,
       C.CHARACTER_SET_NAME
  FROM information_schema.TABLES AS T
  JOIN information_schema.COLUMNS AS C USING (TABLE_SCHEMA, TABLE_NAME)
  JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY AS CCSA
       ON (T.TABLE_COLLATION = CCSA.COLLATION_NAME)
 WHERE TABLE_SCHEMA=SCHEMA()
   AND C.DATA_TYPE IN ('enum', 'varchar', 'char', 'text', 'mediumtext', 'longtext')
 ORDER BY TABLE_SCHEMA,
          TABLE_NAME,
          COLUMN_NAME
;


要查看数据库的默认排序规则:

1
2
USE db_name;
SELECT @@character_set_database, @@collation_database;

要查看表的排序规则:

1
SHOW TABLE STATUS WHERE name LIKE 'table_name';

要查看列的排序规则,请执行以下操作:

1
SHOW FULL COLUMNS FROM TABLE_NAME;


我总是看着SHOW CREATE TABLE mydatabase.mytable

对于数据库,您似乎需要查看SELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA


对于表和列:

1
SHOW CREATE TABLE your_table_name


对于数据库:

1
SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;

示例输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
+----------------------------+---------+--------------------+
| DATABASE                   | charset | collation          |
+----------------------------+---------+--------------------+
| information_schema         | utf8    | utf8_general_ci    |
| drupal_demo1               | utf8    | utf8_general_ci    |
| drupal_demo2               | utf8    | utf8_general_ci    |
| drupal_demo3               | utf8    | utf8_general_ci    |
| drupal_demo4               | utf8    | utf8_general_ci    |
| drupal_demo5               | latin1  | latin1_swedish_ci  |

...

+----------------------------+---------+--------------------+
55 ROWS IN SET (0.00 sec)

mysql>


对于数据库:

1
SHOW CREATE DATABASE"DB_NAME_HERE";

在创建数据库(mysql)时,默认的字符集/排序规则始终是拉丁文,而不是在最初创建数据库时选择了不同的字符集/排序规则。


正如许多人之前所写的,显示完整列应该是获取列信息的首选方法。缺少的是获取字符集的方法,而不直接到达元数据表:

1
2
SHOW FULL COLUMNS FROM my_table WHERE FIELD = 'my_field'
SHOW COLLATION WHERE Collation = 'collation_you_got'