数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
当前回答
我在这里读到,你需要手动转换每个表,这是不正确的。下面是一个如何使用存储过程的解决方案:
DELIMITER $$
DROP PROCEDURE IF EXISTS changeCollation$$
-- character_set parameter could be 'utf8'
-- or 'latin1' or any other valid character set
CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_table_name varchar(255) DEFAULT "";
DECLARE v_message varchar(4000) DEFAULT "No records";
-- This will create a cursor that selects each table,
-- where the character set is not the one
-- that is defined in the parameter
DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');
-- This handler will set the value v_finished to 1
-- if there are no more rows
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN alter_cursor;
-- Start a loop to fetch each rows from the cursor
get_table: LOOP
-- Fetch the table names one by one
FETCH alter_cursor INTO v_table_name;
-- If there is no more record, then we have to skip
-- the commands inside the loop
IF v_finished = 1 THEN
LEAVE get_table;
END IF;
IF v_table_name != '' THEN
IF v_message = 'No records' THEN
SET v_message = '';
END IF;
-- This technic makes the trick, it prepares a statement
-- that is based on the v_table_name parameter and it means
-- that this one is different by each iteration inside the loop
SET @s = CONCAT('ALTER TABLE ',v_table_name,
' CONVERT TO CHARACTER SET ', character_set);
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_message = CONCAT('The table ', v_table_name ,
' was changed to the default collation of ', character_set,
'.\n', v_message);
SET v_table_name = '';
END IF;
-- Close the loop and the cursor
END LOOP get_table;
CLOSE alter_cursor;
-- Returns information about the altered tables or 'No records'
SELECT v_message;
END $$
DELIMITER ;
创建过程后,可以简单地调用它:
CALL changeCollation('utf8');
更多细节请阅读这篇博客。
其他回答
更好的变种生成SQL脚本的SQL请求。它不会破坏默认值/空值。
SELECT concat
(
'ALTER TABLE ',
t1.TABLE_SCHEMA,
'.',
t1.table_name,
' MODIFY ',
t1.column_name,
' ',
t1.column_type,
' CHARACTER SET utf8 COLLATE utf8_general_ci',
if(t1.is_nullable='YES', ' NULL', ' NOT NULL'),
if(t1.column_default is not null, concat(' DEFAULT \'', t1.column_default, '\''), ''),
';'
)
from
information_schema.columns t1
where
t1.TABLE_SCHEMA like 'your_table_here' AND
t1.COLLATION_NAME IS NOT NULL AND
t1.COLLATION_NAME NOT IN ('utf8_general_ci');
要更改表的排序规则,您可以使用,
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8
若要为整个数据库设置默认排序规则,请执行以下操作
ALTER DATABASE `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
否则,
Goto PhpMyAdmin - >操作- >排序。
在这里,您可以找到包含所有现有排序规则的选择框。这样你就可以改变排序规则。在创建新列时,数据库表将遵循这个排序规则。创建新列时不需要选择排序规则。
注意,在改变数据库的字符集/表/列,实际上你可能需要将现有的数据(例如,如果你看到类似“U…Ø·U”ˆØ¨ØªUˆØ±UŠØ¯Ø¬U”)是这样的:
update country set name = convert(cast(convert(name using latin1) as binary) using utf8), cn_flag = convert(cast(convert(cn_flag using latin1) as binary) using utf8), and so on..
对于转换数据库、表和字段,我建议从这个线程中得到这个答案,它会生成一组大的查询,你只需要在粘贴时复制,在这里我还找不到一个自动的解决方案。 还要注意的是,如果你将同一个字段转换两次,你将得到不可恢复的问号:"?? "。如果在转换字段/表之前转换数据,也会得到这个问号。
下面的查询将生成ALTER查询,将所有表中所有适当列的排序规则更改为某种类型(在下面的示例中为utf8_general_ci)。
SELECT concat
(
'ALTER TABLE ',
t1.TABLE_SCHEMA,
'.',
t1.table_name,
' MODIFY ',
t1.column_name,
' ',
t1.data_type,
'(' ,
CHARACTER_MAXIMUM_LENGTH,
')',
' CHARACTER SET utf8 COLLATE utf8_general_ci;'
)
from
information_schema.columns t1
where
t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
t1.COLLATION_NAME IS NOT NULL AND
t1.COLLATION_NAME NOT IN ('utf8_general_ci');
只需运行这个SQL语句就可以一次转换所有数据库表。根据需要更改COLLATION和databaseName。
SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="databaseName"
AND TABLE_TYPE="BASE TABLE";