菜鸟自学lamp笔记三

by kevin 5. 十一月 2014 23:16 >

碰到一个恶心的问题:

无法保存包含中文的字符串,查了很久,就懂的是字符集的问题。

mysql的字符集在server,database,table,column都可以定义。

查看字符集:

数据库级别

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

表级别

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

列级别

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

修改数据库字符集:

   1: ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...];

把表默认的字符集和所有字符列(CHAR,VARCHAR,TEXT)改为新的字符集:

   1: ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name [COLLATE ...]

只是修改表的默认字符集:

   1: ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];

修改列的字符集:

   1: ALTER TABLE tbl_name CHANGE c_name c_name CHARACTER SET character_name [COLLATE ...];

Tags: ,

lamp

分享到: 更多