如何将数据库中的所有表转换为一个归类?

我收到错误:

非法混合sorting(utf8_general_ci,IMPLICIT)和(utf8_unicode_ci,IMPLICIT)进行操作'='“

我试图手动更改两个表到utf8_general_ci,IMPLICIT但我仍然得到错误。

有没有办法将所有表转换为utf8_general_ci,IMPLICIT并完成它?

您需要为每个表执行一个alter table语句。 声明将遵循这种forms:

 ALTER TABLE tbl_name [[DEFAULT] CHARACTER SET charset_name] [COLLATE collation_name] 

现在要获取数据库中的所有表,您需要执行以下查询:

 SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="YourDataBaseName" AND TABLE_TYPE="BASE TABLE"; 

所以现在让MySQL为你写代码:

 SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE your_collation_name_here;") AS ExecuteTheString FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="YourDatabaseName" AND TABLE_TYPE="BASE TABLE"; 

您可以复制结果并执行它们。 我没有testing语法,但你应该能够找出其余的。 把它看作是一个小练习。

希望帮助!

更好的选项也可以更改表内varchar列的sorting规则

 SELECT CONCAT('ALTER TABLE `', TABLE_NAME,'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') AS mySQL FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= "myschema" AND TABLE_TYPE="BASE TABLE" 

另外,如果你在启动串脚本之前有非UTF8列的foref键数据

 SET foreign_key_checks = 0; 

这意味着全球SQL将为mySQL:

 SET foreign_key_checks = 0; ALTER TABLE `table1` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE `table2` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE `tableXXX` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; SET foreign_key_checks = 1; 

但要小心,如果根据mysql文档http://dev.mysql.com/doc/refman/5.1/en/charset-column.html

如果使用ALTER TABLE将一个字符集中的列转换为另一个字符集,MySQL会尝试映射数据值,但如果字符集不兼容,则可能会丢失数据。 “

编辑:特别是与列types枚举,它只是完全崩溃枚举集(即使没有特殊的字符) https://bugs.mysql.com/bug.php?id=26731

以下是更准确的查询。 我举例如何将其转换为utf8

 SELECT CONCAT("ALTER TABLE `", TABLE_NAME,"` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;") AS mySQL FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="myschema" AND TABLE_TYPE="BASE TABLE" 

@Nanphibian的build议帮了我很多…
进一步,并添加专栏和意见的脚本

只需在下面input模式名称,剩下的就完成了

 -- set your table name here SET @MY_SCHEMA = ""; -- tables SELECT DISTINCT CONCAT("ALTER TABLE ", TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as queries FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=@MY_SCHEMA AND TABLE_TYPE="BASE TABLE" UNION -- table columns SELECT DISTINCT CONCAT("ALTER TABLE ", C.TABLE_NAME, " CHANGE ", C.COLUMN_NAME, " ", C.COLUMN_NAME, " ", C.COLUMN_TYPE, " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as queries FROM INFORMATION_SCHEMA.COLUMNS as C LEFT JOIN INFORMATION_SCHEMA.TABLES as T ON C.TABLE_NAME = T.TABLE_NAME WHERE C.COLLATION_NAME is not null AND C.TABLE_SCHEMA=@MY_SCHEMA AND T.TABLE_TYPE="BASE TABLE" UNION -- views SELECT DISTINCT CONCAT("CREATE OR REPLACE VIEW ", V.TABLE_NAME, " AS ", V.VIEW_DEFINITION, ";") as queries FROM INFORMATION_SCHEMA.VIEWS as V LEFT JOIN INFORMATION_SCHEMA.TABLES as T ON V.TABLE_NAME = T.TABLE_NAME WHERE V.TABLE_SCHEMA=@MY_SCHEMA AND T.TABLE_TYPE="VIEW"; 

如果你想复制粘贴bash脚本:

 var=$(mysql -e 'SELECT CONCAT("ALTER TABLE ", TABLE_NAME," CONVERT TO CHARACTER SET utf8 COLLATE utf8_czech_ci;") AS execTabs FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="zabbix" AND TABLE_TYPE="BASE TABLE"' -uroot -p ) var+='ALTER DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_general_ci;' echo $var | cut -d " " -f2- | mysql -uroot -p zabbix 

将zabbix更改为您的数据库名称。

你可以使用这个BASH脚本:

 #!/bin/bash USER="YOUR_DATABASE_USER" PASSWORD="YOUR_USER_PASSWORD" DB_NAME="DATABASE_NAME" CHARACTER_SET="utf8" # your default character set COLLATE="utf8_general_ci" # your default collation tables=`mysql -u $USER -p$PASSWORD -e "SELECT tbl.TABLE_NAME FROM information_schema.TABLES tbl WHERE tbl.TABLE_SCHEMA = '$DB_NAME' AND tbl.TABLE_TYPE='BASE TABLE'"` for tableName in $tables; do if [[ "$tableName" != "TABLE_NAME" ]] ; then mysql -u $USER -p$PASSWORD -e "ALTER TABLE $DB_NAME.$tableName DEFAULT CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" echo "$tableName - done" fi done 

这是我的bash脚本版本。 它将数据库名称作为参数,并将所有表转换为另一个字符集和归类(由脚本中定义的另一个参数或默认值给定)。

 #!/bin/bash # mycollate.sh <database> [<charset> <collation>] # changes MySQL/MariaDB charset and collation for one database - all tables and # all columns in all tables DB="$1" CHARSET="$2" COLL="$3" [ -n "$DB" ] || exit 1 [ -n "$CHARSET" ] || CHARSET="utf8mb4" [ -n "$COLL" ] || COLL="utf8mb4_general_ci" echo $DB echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql echo "USE $DB; SHOW TABLES;" | mysql -s | ( while read TABLE; do echo $DB.$TABLE echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB done )