如何将数据库中的所有表转换为一个排序规则?

时间:2012-06-02 05:12:17

标签: mysql sql

我收到错误:

  

操作'='的非法混合排序(utf8_general_ci,IMPLICIT)和(utf8_unicode_ci,IMPLICIT)

我尝试将这两个表手动更改为utf8_general_ci,IMPLICIT,但我仍然收到错误。

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

11 个答案:

答案 0 :(得分:133)

您需要为每个表执行alter table语句。声明将遵循以下形式:

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";

您可以复制结果并执行它们。我没有测试语法,但你应该能够弄清楚其余部分。把它想象成一个小练习。

希望有帮助!

答案 1 :(得分:46)

更好的选项还可以更改表中的varchar列的整理

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列上有forein键的数据,那么使用

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会尝试映射数据值,但如果字符集不兼容,则可能会丢失数据。 "

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

答案 2 :(得分:19)

@ Namphibian的建议帮了我很多...
更进一步,并为脚本添加了列和视图

只需在下方输入您的架构名称,然后执行其余操作

[POST]
/destinationpool/{destinationpool-name}/destinations
(url).../destinationpool/{destinationpool-name}/destinations?destination-name=<name>

答案 3 :(得分:17)

以下是更准确的查询。 我举例说明如何将其转换为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"

答案 4 :(得分:7)

如果您使用的是PhpMyAdmin,则现在可以:

  1. 选择数据库。
  2. 点击“操作”标签。
  3. 在“排序规则”部分下,选择所需的排序规则。
  4. 单击“更改所有表排序规则”复选框。
  5. 将出现一个新的“更改所有表列归类”复选框。
  6. 单击“更改所有表列归类”复选框。
  7. 点击“开始”按钮。

我要转换250多个表。花了5分钟多一点。

答案 5 :(得分:6)

您可以使用此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

答案 6 :(得分:1)

对于phpMyAdmin,我知道了这一点:

SELECT GROUP_CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" SEPARATOR ' ') AS    OneSQLString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="yourtableschemaname"
AND TABLE_TYPE="BASE TABLE"

只需更改您的表名称,就可以了。

答案 7 :(得分:0)

如果你想要一个复制粘贴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更改为您的数据库名称。

答案 8 :(得分:0)

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

#!/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
)

答案 9 :(得分:0)

通过添加密码变量,使@Petr Stastny的答案更进一步。我希望它实际上像普通密码一样接受而不是作为参数输入,但是它可以满足我的需要。

#!/bin/bash

# mycollate.sh <database> <password> [<charset> <collation>]
# changes MySQL/MariaDB charset and collation for one database - all tables and
# all columns in all tables

DB="$1"
PW="$2"
CHARSET="$3"
COLL="$4"

[ -n "$DB" ] || exit 1
[ -n "$PW" ]
[ -n "$CHARSET" ] || CHARSET="utf8mb4"
[ -n "$COLL" ] || COLL="utf8mb4_bin"

PW="--password=""$PW"

echo $DB
echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql -u root "$PW"

echo "USE $DB; SHOW TABLES;" | mysql -s "$PW" | (
    while read TABLE; do
        echo $DB.$TABLE
        echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql "$PW" $DB
    done
)

PW="pleaseEmptyMeNow"

答案 10 :(得分:0)

从G H开始,我添加了用户和主机参数,以防您需要在远程服务器上执行此操作

    #!/bin/bash

    # mycollate.sh <database> <user> <password> [<host> <charset> <collation>]
    # changes MySQL/MariaDB charset and collation for one database - all tables and
    # all columns in all tables

    DB="$1"
    USER="$2"
    PW="$3"
    HOST="$4"
    CHARSET="$5"
    COLL="$6"

    [ -n "$DB" ] || exit 1
    [ -n "$USER" ] || exit 1
    [ -n "$PW" ] || exit 1
    [ -n "$HOST" ] || HOST="localhost"
    [ -n "$CHARSET" ] || CHARSET="utf8mb4"
    [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

    PW="--password=""$PW"
    HOST="--host=""$HOST"
    USER="--user=""$USER"

    echo $DB
    echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql "$HOST" "$USER" "$PW"

    echo "USE $DB; SHOW TABLES;" | mysql  "$HOST" "$USER" "$PW" | (
        while read TABLE; do
            echo $DB.$TABLE
            echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql  "$HOST" "$USER" "$PW" $DB
        done
    )

    PW="pleaseEmptyMeNow"