数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
数据库现在是latin1_general_ci,我想将排序规则更改为utf8mb4_general_ci。
在PhpMyAdmin中是否有任何设置来更改数据库,表,列的排序规则?而不是一个一个地改变?
当前回答
快速方法-导出到SQL文件,使用搜索和替换来更改您需要更改的文本。创建新数据库,导入数据,然后将旧数据库和新数据库重命名为旧名称。
其他回答
要更改表的排序规则,您可以使用,
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8
若要为整个数据库设置默认排序规则,请执行以下操作
ALTER DATABASE `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
否则,
Goto PhpMyAdmin - >操作- >排序。
在这里,您可以找到包含所有现有排序规则的选择框。这样你就可以改变排序规则。在创建新列时,数据库表将遵循这个排序规则。创建新列时不需要选择排序规则。
你可以运行一个php脚本。
<?php
$con = mysql_connect('localhost','user','password');
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db('dbname');
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}}
echo "The collation of your database has been successfully changed!";
?>
修改数据库中所有表中所有字段的排序规则。
我只是通过前面提到的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!";
?>}
您可以简单地将此代码添加到脚本文件
//Database Connection
$host = 'localhost';
$db_name = 'your_database_name';
$db_user = 'your_database_user_name';
$db_pass = 'your_database_user_password';
$con = mysql_connect($host,$db_user,$db_pass);
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db($db_name);
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}
}
echo "The collation of your database has been successfully changed!";
下面的查询将生成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');