数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
当前回答
更好的变种生成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');
其他回答
更好的变种生成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');
我在这里读到,你需要手动转换每个表,这是不正确的。下面是一个如何使用存储过程的解决方案:
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');
更多细节请阅读这篇博客。
我很惊讶地发现,所以我不得不回到这里报告,优秀和维护良好的Interconnect/it SAFE SEARCH and REPLACE ON DATABASE脚本有一些选项可以将表转换为utf8 / unicode,甚至转换为innodb。它是一个脚本,通常用于将数据库驱动的网站(Wordpress, Drupal, Joomla等)从一个域迁移到另一个域。
https://github.com/interconnectit/Search-Replace-DB https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
您可以通过PHP脚本更改所有表的CHARSET和COLLATION,如下所示。我喜欢hkasera的答案,但它的问题是查询在每个表上运行两次。这段代码几乎是一样的,除了使用MySqli而不是mysql和防止双重查询。如果我可以投票的话,我会给hkasera的答案投票。
<?php
$conn1=new MySQLi("localhost","user","password","database");
if($conn1->connect_errno){
echo mysqli_connect_error();
exit;
}
$res=$conn1->query("show tables") or die($conn1->error);
while($tables=$res->fetch_array()){
$conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
}
echo "The collation of your database has been successfully changed!";
$res->free();
$conn1->close();
?>
修改数据库中所有表中所有字段的排序规则。
我只是通过前面提到的Php为表中的字段添加另一个循环到解决方案。这很有帮助,表中的所有字段也都转换了。
<?php
$con = mysql_connect('localhost','user','pw');
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db('database_name');
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $table) { // for each table
$sql = "ALTER TABLE $table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
echo "\n".$sql;
mysql_query($sql);
$sql = "show fields in ".$table." where type like 'varchar%' or type like 'char%' or type='text' or type='mediumtext';";
$rs2=mysql_query($sql);
while( $rw2 = mysql_fetch_array($rs2) ){ // for each field in table
$sql = "ALTER TABLE `".$table."` CHANGE `".$rw2['Field']."` `".$rw2['Field']."` ".$rw2['Type']." CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
echo "\n".$sql;
mysql_query($sql);
}
}
}
echo "The collation of your database has been successfully changed!";
?>}