Answers:
这是我的做法-
对于模式(或数据库-它们是同义词):
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";
schemaname
可能只是数据库名称。
对于列:
SHOW FULL COLUMNS FROM table_name;
latin1_swedish_ci
,则字符集只能是latin1
。如果排序规则是utf8mb4_general_ci
,字符集除了不能是其他字符utf8mb4
。
对于数据库:
USE your_database_name;
show variables like "character_set_database";
-- or:
-- show variables like "collation_database";
cf. 此页。并查看MySQL手册
对于服务器上的所有数据库:
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 |
+----------------------------+---------+
获取表的排序规则:
mysql> USE my_database;
mysql> SHOW TABLE STATUS WHERE NAME LIKE 'my_tablename';
或-将输出用于创建表的完整SQL:
mysql> show create table my_tablename
获取列的排序规则:
mysql> SHOW FULL COLUMNS FROM my_tablename;
输出:
+---------+--------------+--------------------+ ....
| field | type | collation |
+---------+--------------+--------------------+ ....
| id | int(10) | (NULL) |
| key | varchar(255) | latin1_swedish_ci |
| value | varchar(255) | latin1_swedish_ci |
+---------+--------------+--------------------+ ....
type
?我有mysql 5.7.9,并type
显示data type
的列而不是character set
。有些像 int(10)
varchar(255)
...等等而不是utf8
type
为data type
@MTK,也许上面是“输出:”部分中的复制粘贴错误。
对于表:
SHOW TABLE STATUS
将列出所有表格。
筛选使用:
SHOW TABLE STATUS where name like 'table_123';
SELECT TABLE_SCHEMA, TABLE_NAME, CCSA.CHARACTER_SET_NAME AS DEFAULT_CHAR_SET, COLUMN_NAME, COLUMN_TYPE, C.CHARACTER_SET_NAME 来自information_schema.TABLES AS T 使用信息(TABLE_SCHEMA,TABLE_NAME)加入INFORMATION_SCHEMA.COLUMNS 以CCSA身份加入information_schema.COLLATION_CHARACTER_SET_APPLICABILITY 开启(T.TABLE_COLLATION = CCSA.COLLATION_NAME) 在哪里TABLE_SCHEMA = SCHEMA() AND C.DATA_TYPE IN('enum','varchar','char','text','mediumtext','longtext') 按TABLE_SCHEMA排序, TABLE_NAME, COLUMN_NAME ;
我总是只看SHOW CREATE TABLE mydatabase.mytable
。
对于数据库,似乎需要查看SELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA
。
select default_character_set_name from information_schema.schemata
还不够,因为您无法确定哪一行与哪个数据库相关。使用select default_character_set_name,schema_name from information_schema.schemata
或简单地:select*from information_schema.schemata
。
SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '<database-name>' \G;
而且效果很好:)谢谢!
对于表和列:
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";
在创建数据库(MySQL)时,默认字符集/排序规则始终为LATIN,而是在最初创建数据库时选择了其他字符集/排序规则
latin1_swedish_ci
因为MySQL的创建者Monty Widenius是瑞典人,开始时并没有想像中的那样。
如许多人先前所写,SHOW FULL COLUMNS应该是获取列信息的首选方法。缺少的是在此之后无需直接访问元数据表即可获取字符集的方法:
SHOW FULL COLUMNS FROM my_table WHERE Field = 'my_field'
SHOW COLLATION WHERE Collation = 'collation_you_got'
SHOW CREATE TABLE
讨论的表格