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

时间:2009-06-26 15:22:20

标签: sql mysql unicode character-encoding collation

什么是(默认)字符集:

  • MySQL数据库

  • MySQL表

  • MySQL专栏

15 个答案:

答案 0 :(得分:663)

我是怎么做的 -

For Schemas:

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";

答案 1 :(得分:438)

SHOW FULL COLUMNS FROM table_name;

答案 2 :(得分:212)

对于数据库

USE your_database_name;
show variables like "character_set_database";
-- or:
-- show variables like "collation_database";

比照。 this page。并查看MySQL手册

答案 3 :(得分:117)

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

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';

OR - 将输出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  |
+---------+---------+--------------------+ ....

答案 4 :(得分:62)

对于表格

SHOW TABLE STATUS将列出所有表格。

使用以下过滤:

SHOW TABLE STATUS where name like 'table_123';

答案 5 :(得分:44)

对于数据库

只需使用以下命令:

USE db_name;
SELECT @@character_set_database;
-- or:
-- SELECT @@collation_database;

答案 6 :(得分:30)

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
;

答案 7 :(得分:23)

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

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

要查看表格的整理:

SHOW TABLE STATUS where name like 'table_name';

要查看列的整理:

SHOW FULL COLUMNS FROM table_name;

答案 8 :(得分:22)

我总是只看SHOW CREATE TABLE mydatabase.mytable

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

答案 9 :(得分:20)

对于表格

show create table your_table_name

答案 10 :(得分:17)

对于数据库

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> 

答案 11 :(得分:15)

对于数据库

SHOW CREATE DATABASE "DB_NAME_HERE";

在创建数据库(MySQL)时,默认字符集/排序规则始终是LATIN,而是在最初创建数据库时选择了另一个

答案 12 :(得分:4)

正如之前所写的那样,SHOW FULL COLUMNS应该是获取列信息的首选方法。 缺少什么是在没有直接到达元数据表的情况下获取字符集的方法:

SHOW FULL COLUMNS FROM my_table WHERE Field = 'my_field'
SHOW COLLATION WHERE Collation = 'collation_you_got'

答案 13 :(得分:2)

对于数据库: USE db_name; SELECT @@character_set_database;

答案 14 :(得分:0)

显示全局变量,其中variable_name如'character_set_%'或variable_name如'collat​​ion%'