我知道表和数据库的语言有很多设置。
我已经创建了数据库。我相信当我创建它时,它是默认/ LATIN。我想改变一切 - 我的意思是...... 表和数据库,UTF-8 。
我该怎么做?感谢。
答案 0 :(得分:51)
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
答案 1 :(得分:4)
查看Using alter command to change character set。
另一个有用的链接:http://dev.mysql.com/doc/refman/5.0/en/charset-table.html
一般表格是
ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]
以及表格中的特定列
ALTER TABLE column COLLATE utf8_general_ci
答案 2 :(得分:4)
aioobe的回答讲述了如何更改数据库,表或列的字符集。你应该记住
设置表的字符集只是指定该表中新列的默认字符集。它不会更改预先存在的列的字符集;您必须单独执行这些列,或者如果要将表中的每个字符串类型列更改为相同的字符集,则可以使用命令执行此操作:“alter table ... convert to character set”( http://dev.mysql.com/doc/refman/5.1/en/alter-table.html)
如果您已经在列中存储了错误编码的数据,那么使用“alter table ... modify”更改列将无法完全解决问题。例如,如果您将UTF-8数据存储在Latin1列中,并且您将字符集直接从Latin1更改为UTF-8,那么之后它仍然会被错误编码。这可以通过二进制从Latin-1转换为UTF-8来解决。
答案 3 :(得分:0)
1)数据库默认字符集和整理:
SELECT @@character_set_database, @@collation_database;
通过以下方式修改:ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
2)表格默认字符集和整理:
SELECT T.table_name, 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 = "YOUR_DB";`
通过以下方式修改:ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci
3)列字符集和整理:
SELECT c.TABLE_NAME, c.COLUMN_NAME, c.CHARACTER_SET_NAME, c.COLLATION_NAME
FROM information_schema.COLUMNS c
WHERE c.table_schema = "YOUR_DB";`
通过以下方式修改:ALTER TABLE [table_name] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
第三个要求您禁用数据转换的外键检查。所以把这一切放在一起:
DELIMITER //
CREATE PROCEDURE migrate_charset_to_utf8()
BEGIN
DECLARE done TINYINT DEFAULT 0;
DECLARE curr_table VARCHAR(64);
DECLARE table_cursor CURSOR FOR
SELECT T.table_name
FROM information_schema.TABLES T
WHERE T.TABLE_TYPE = 'BASE TABLE' AND
T.TABLE_SCHEMA = 'YOUR_DB';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN table_cursor;
table_loop: LOOP
FETCH table_cursor INTO curr_table;
IF done THEN
LEAVE table_loop;
END IF;
# Convert table data(columns) charset
SET @sql_str1 = CONCAT("ALTER TABLE ", curr_table, " CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
PREPARE stmt1 FROM @sql_str1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
# Set table's default charset e.g for new columns added
SET @sql_str2 = CONCAT("ALTER TABLE ", curr_table, " CHARACTER SET utf8 COLLATE utf8_general_ci");
PREPARE stmt2 FROM @sql_str2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
END LOOP table_loop;
CLOSE table_cursor;
END//
DELIMITER ;
SET @@FOREIGN_KEY_CHECKS = 0;
CALL migrate_charset_to_utf8();
SET @@FOREIGN_KEY_CHECKS = 1;
ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
编辑:改为here
答案 4 :(得分:-2)
添加到您的my.cnf:
[mysqld]
character-set-server=utf8
default-collation=utf8_unicode_ci
然后重启mysqld deamon。
<强>增加:强>
ALTER DATABASE your_base_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;
和my.cnf
SET collation_connection = utf8_unicode_ci;
SET character_set_results = utf8;
SET character_set_connection = utf8;
SET character_set_client = utf8;