如何查看MySQL数据库/表/列是什么字符集?
什么是(默认)字符集:
-
MySQL数据库
-
MySQL表
-
MySQL列
这是我该怎么做的 –
对于模式:
SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "schemaname";
对于表格:
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";
对于列:
SELECT character_set_name FROM information_schema.`COLUMNS` WHERE table_schema = "schemaname" AND table_name = "tablename" AND column_name = "columnname";
对于列 :
SHOW FULL COLUMNS FROM table_name;
对于数据库 :
USE your_database_name; show variables like "character_set_database"; -- or: -- show variables like "collation_database";
参看 这个页面 。 并查看MySQL手册
对于表格 :
SHOW TABLE STATUS
将列出所有的表格。
过滤使用:
SHOW TABLE STATUS where name like 'table_123';
对于数据库 :
只要使用这些命令:
USE db_name; SELECT @@character_set_database; -- or: -- SELECT @@collation_database;
对于服务器上的所有数据库 :
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 | | my_database | latin1 | latin1_swedish_ci | ... +----------------------------+---------+--------------------+
对于单个数据库 :
mysql> USE my_database; mysql> show variables like "character_set_database";
输出:
+----------------------------+---------+ | Variable_name | Value | +----------------------------+---------+ | character_set_database | latin1 | +----------------------------+---------+
获取表格的sorting规则:
mysql> USE my_database; mysql> SHOW TABLE STATUS WHERE NAME LIKE 'my_tablename';
或者 – 将输出create table的完整SQL:
mysql> show create table my_tablename
获取列的整理:
mysql> SHOW FULL COLUMNS FROM my_tablename;
输出:
+---------+---------+--------------------+ .... | field | type | collation | +---------+---------+--------------------+ .... | id | utf8 | (NULL) | | key | utf8 | latin1_swedish_ci | | value | utf8 | latin1_swedish_ci | +---------+---------+--------------------+ ....
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 joininformation_schema.COLUMNS AS C USING(TABLE_SCHEMA,TABLE_NAME) joininformation_schema.COLLATION_CHARACTER_SET_APPLICABILITY作为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 ;
我总是只看SHOW CREATE TABLE mydatabase.mytable
。
对于数据库,看起来你需要查看SELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA
。
对于表和列 :
show create table your_table_name
对于数据库 :
SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
示例输出:
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>
对于数据库 :
SHOW CREATE DATABASE "DB_NAME_HERE";
在创build一个数据库(MySQL)时,默认字符集/sorting规则始终是LATIN,而不是在最初创build数据库时select了另一个