A script to change all tables and fields to the utf-8-bin collation in MYSQL

后端 未结 16 1027
有刺的猬
有刺的猬 2020-12-04 06:24

Is there a SQL or PHP script that I can run that will change the default collation in all tables and fields in a database?

I can write one

16条回答
  •  青春惊慌失措
    2020-12-04 06:46

    Can be done in a single command (rather than 148 of PHP):

    mysql --database=dbname -B -N -e "SHOW TABLES" \
    | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' \
    | mysql --database=dbname &
    

    You've got to love the commandline... (You might need to employ the --user and --password options for mysql).

    EDIT: to avoid foreign key problems, added SET foreign_key_checks = 0; and SET foreign_key_checks = 1;

提交回复
热议问题