简体   繁体   English

如何为数据库中创建的所有表设置默认排序规则?

[英]How do I set default collation for all tables that are created in a database?

I have a database dump taken from a broken database that I need to import into a clean installation. 我有一个从损坏的数据库中取出的数据库转储,我需要将其导入到干净的安装中。 However, the original database seem to have been set to use utf8_unicode_ci. 但是,原始数据库似乎已设置为使用utf8_unicode_ci。

When importing this dump into clean database, all databases are created with the default utf8_general_ci which gives me duplicate entries for words including ß , because general makes 'ß' == 's' , while as utf8_unicode_ci supposedly have 'ß' == 'ss' . 将此转储导入干净数据库时,所有数据库都使用默认的utf8_general_ci创建,它为包含ß单词提供重复条目,因为一般会使'ß' == 's' ,而utf8_unicode_ci应该具有'ß' == 'ss'

But, when importing mysql always seems to select the default utf8_general_ci when creating the table, even though I set the database (schema) default to utf8_unicode_ci. 但是,当导入mysql时,似乎总是在创建表时选择默认的utf8_general_ci,即使我将数据库(schema)默认设置为utf8_unicode_ci。

Is there any way to force it to create tables with utf8_unicode_ci without having to inject alter table statements in my dump? 有没有办法强制它使用utf8_unicode_ci创建表而不必在我的转储中注入alter table语句? It is several GB in size and is gonna be a pain to modify manually. 它的大小是几GB,手动修改会很痛苦。

Configuring MySQL system wide is fine. 配置MySQL系统范围很好。

I tried setting: 我尝试过设置:

collation-server=utf8_unicode_ci

in my.cnf but that doesn't seem to set the default collation for table creation. 在my.cnf中,但似乎没有设置表创建的默认排序规则。

I was working on this same issue this morning and I was able to get the table collation set the following way. 今天早上我正在处理同样的问题,我能够按照以下方式设置表格排序。

DROP TABLE IF EXISTS `agtAgentTypes`;
CREATE TABLE `agtAgentTypes` (
  `agentTypeID` int(11) NOT NULL,
  `agentType` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`agentTypeID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;

For some reason it seems that engine and charset need an = but collation trips up if it has one. 出于某种原因,似乎引擎和字符集需要一个=但是如果它有一个校对,则会进行校对。 I tried it out because I noticed the MySQL Charset Examples were also not using the =. 我尝试了,因为我注意到MySQL Charset示例也没有使用=。

This was tested against MySQL Community Server 5.5.32-cll-lve. 这是针对MySQL社区服务器5.5.32-cll-lve进行测试的。

Apparently there is no way of forcing collation on newly created tables if you specify charset in your create statements, meaning that if you have: 如果你在create语句中指定了charset,显然没有办法在新创建的表上强制排序,这意味着如果你有:

CREATE TABLE foo
...
CHARSET=utf8;

It will implicitly set it to utf8_general_ci which is the default collation for that charset. 它将隐式将其设置为utf8_general_ci,这是该charset的默认排序规则。 This is regardless of database settings, system settings and connection settings. 这与数据库设置,系统设置和连接设置无关。

I ended up invoking this: 我最后调用了这个:

 cat dump.sql|sed s/CHARSET=utf8/CHARSET=utf8\ COLLATE=utf8_unicode_ci/ > dump_replaced.sql

and just waited. 等等。

if you are using phpmyadmin then read below article: 如果您使用的是phpmyadmin,请阅读以下文章:

http://kb.mediatemple.net/questions/138/Default+MySQL+character+set+and+collation#gs http://kb.mediatemple.net/questions/138/Default+MySQL+character+set+and+collat​​ion#gs

OR try this way 或尝试这种方式

CREATE TABLE tbl_name (column_list)
    [[DEFAULT] CHARACTER SET charset_name]
    [COLLATE collation_name]]

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

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM