如何更改数据库,表,列的sorting规则?
现在数据库是latin1_general_ci
,我想将sorting规则更改为utf8_general_ci
。 在PhpMyAdmin中有更改数据库,表,列的sorting规则吗? 而不是一个一个的改变?
您需要分别转换每个表格:
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8
(这也会转换列),或者使用latin1
导出数据库并使用utf8
导入。
你可以运行一个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!"; ?>
我在这里作出贡献,正如OP所说:
如何更改数据库,表,列的sorting规则?
所选的答案只是在表级别上表示。
改变数据库范围:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
每桌更改:
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
好的做法是在表格级别更改它,因为它也会更改列。 特定列的更改适用于任何特定情况。
更改特定列的sorting规则:
ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
要单独更改表格的sorting规则,可以使用,
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8
要为整个数据库设置默认sorting规则,
ALTER DATABASE `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
要不然,
转到PhpMyAdmin->操作 – >整理。
在那里你可以find包含所有现有sorting规则的select框。 所以在这里你可以改变你的sorting规则。 所以在这之后,数据库表将在创build新列时遵循此sorting规则。 创build新列时不需要selectsorting规则。
您可以在几个级别上设置默认sorting规则:
http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html
1)客户端2)服务器默认3)数据库默认4)表默认5)列
如果您运行phpMyAdmin >>select数据库>>select表格>>转到“操作”选项卡>>在“表格选项”部分>>您可以从下拉列表中select整理>>,一旦您按{Go}屏幕上方会显示一条消息:
您的SQL查询已成功执行
和一个脚本
ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
但是它不会改变现有列的sorting规则。 要做到这一点,你可以使用这个脚本(这个也来自phpMyAdmin)
ALTER TABLE `tableName` CHANGE `Name` `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
您可以通过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(); ?>
我在这里阅读它,你需要手动转换每个表,这是不正确的。 这里是一个解决scheme如何使用存储过程来做到这一点:
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 ;
程序创build后,简单地调用它:
CALL changeCollation('utf8');
欲了解更多详情阅读这个博客 。
以下查询将生成ALTER查询,将所有表中所有适当列的sorting规则更改为某种types(下例中的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');
您可以简单地将此代码添加到脚本文件
//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!";
我很惊讶,所以我不得不回到这里,并报告说,优秀和维护良好的互连/安全search和更换数据库脚本有一些选项转换表utf8 / unicode,甚至转换为innodb 。 这是一个通常用于将数据库驱动的网站(Wordpress,Drupal,Joomla等)从一个域迁移到另一个域的脚本。
如果您想更新架构上的默认字符集:
ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
快速的方式 – 导出到SQL文件,使用search和replace来更改您需要更改的文本。 创build新数据库,导入数据,然后将旧数据库和新数据库重命名为旧名称。
我使用了下面的shell脚本。 它将数据库名称作为参数,并将所有表转换为另一个字符集和归类(由脚本中定义的另一个参数或默认值给定)。
#!/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 )
只要运行这个SQL。 将您的COLLATION更改为您所需的和databaseName。
SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS ExecuteTheString FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="databaseName" AND TABLE_TYPE="BASE TABLE";