我见过的每一篇关于SO的帖子都表明运行以下SQL:
ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Run Code Online (Sandbox Code Playgroud)
除非我弄错了,否则它的问题在于它明确指定列排序规则,因此当你mysqldump数据库时你最终得到这样的东西:
`address` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`zipcode` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
Run Code Online (Sandbox Code Playgroud)
我的问题是..有没有办法将列排序规则转换为表或数据库默认值而不执行此操作?
例如,我有可能看起来像这样的表:
`address` varchar(150) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(2) COLLATE utf8_general_ci DEFAULT NULL,
`zipcode` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
Run Code Online (Sandbox Code Playgroud)
我想要的是将所有列转换为utf8_unicode_ci(表/数据库默认值),但不要将每列显式设置为该排序规则,这样当我mysqldump转换后的表时,它看起来像这样:
`address` varchar(150) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(2) DEFAULT NULL,
`zipcode` varchar(10) DEFAULT NULL,
Run Code Online (Sandbox Code Playgroud)
在表创建语句末尾有一行,用于定义默认字符集和排序规则: ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
如果您的表或列与 MySQL 默认值不同(在我的例子中为 latin1_sweedish_ci),那么它将打印出与该列的排序规则。请参阅以下实验来证明这一点。
要设置默认字符集,请参阅这篇文章。
首先,我们创建一个包含两个表的数据库。一张表指定了字符集和排序规则。
mysql> create database SO;
mysql> use SO;
mysql> create table test1 (col1 text, col2 text);
mysql> create table test2 (col1 text, col2 text) character set utf8 collate utf8_unicode_ci;
Run Code Online (Sandbox Code Playgroud)
现在检查show create table看看它是什么样子的:
mysql> show create table test1;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test1 | CREATE TABLE `test1` (
`col1` text,
`col2` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1
+-------+-----------------+
1 row in set (0.00 sec)
mysql> show create table test2;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test2 | CREATE TABLE `test2` (
`col1` text COLLATE utf8_unicode_ci,
`col2` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
我们看到,test2这些列看起来已经是专门指定的,而不是使用默认值。我怀疑如果它与 MySQL 默认值不同,它会列出它,而不是如果它与表默认值不同。现在让我们看看它们在 information_schema 数据库中的样子。
mysql> select table_schema, table_name, table_collation from information_schema.tables where table_schema = 'SO';
+--------------+------------+-------------------+
| table_schema | table_name | table_collation |
+--------------+------------+-------------------+
| SO | test1 | latin1_swedish_ci |
| SO | test2 | utf8_unicode_ci |
+--------------+------------+-------------------+
2 rows in set (0.00 sec)
mysql> select table_schema, table_name, column_name, character_set_name, collation_name from information_schema.columns where table_schema = 'SO';
+--------------+------------+-------------+--------------------+-------------------+
| table_schema | table_name | column_name | character_set_name | collation_name |
+--------------+------------+-------------+--------------------+-------------------+
| SO | test1 | col1 | latin1 | latin1_swedish_ci |
| SO | test1 | col2 | latin1 | latin1_swedish_ci |
| SO | test2 | col1 | utf8 | utf8_unicode_ci |
| SO | test2 | col2 | utf8 | utf8_unicode_ci |
+--------------+------------+-------------+--------------------+-------------------+
4 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
看起来这些列都有特定的字符集和排序规则,无论我们是否指定它。让我们将 test1 更新为首选字符集和排序规则,看看会发生什么。
mysql> ALTER TABLE test1 CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test1;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test1 | CREATE TABLE `test1` (
`col1` mediumtext COLLATE utf8_unicode_ci,
`col2` mediumtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
mysql> show create table test2;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test2 | CREATE TABLE `test2` (
`col1` text COLLATE utf8_unicode_ci,
`col2` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
现在他们都将整理结果放入show create table声明中。让我们再次检查 information_schema。
mysql> select table_schema, table_name, table_collation from information_schema.tables where table_schema = 'SO';
+--------------+------------+-----------------+
| table_schema | table_name | table_collation |
+--------------+------------+-----------------+
| SO | test1 | utf8_unicode_ci |
| SO | test2 | utf8_unicode_ci |
+--------------+------------+-----------------+
2 rows in set (0.00 sec)
mysql> select table_schema, table_name, column_name, character_set_name, collation_name from information_schema.columns where table_schema = 'SO';
+--------------+------------+-------------+--------------------+-----------------+
| table_schema | table_name | column_name | character_set_name | collation_name |
+--------------+------------+-------------+--------------------+-----------------+
| SO | test1 | col1 | utf8 | utf8_unicode_ci |
| SO | test1 | col2 | utf8 | utf8_unicode_ci |
| SO | test2 | col1 | utf8 | utf8_unicode_ci |
| SO | test2 | col2 | utf8 | utf8_unicode_ci |
+--------------+------------+-------------+--------------------+-----------------+
4 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
看起来都差不多。但是,当我们向两个表添加额外的列时会发生什么?
mysql> alter table test1 add column col3 text;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test2 add column col3 text;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test1;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test1 | CREATE TABLE `test1` (
`col1` mediumtext COLLATE utf8_unicode_ci,
`col2` mediumtext COLLATE utf8_unicode_ci,
`col3` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
mysql> show create table test2;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test2 | CREATE TABLE `test2` (
`col1` text COLLATE utf8_unicode_ci,
`col2` text COLLATE utf8_unicode_ci,
`col3` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,他们都从表中获取排序规则。因此,不必太担心后来添加的列会出现问题。让我们再检查一次 information_schema...
mysql> select table_schema, table_name, table_collation from information_schema.tables where table_schema = 'SO';
+--------------+------------+-----------------+
| table_schema | table_name | table_collation |
+--------------+------------+-----------------+
| SO | test1 | utf8_unicode_ci |
| SO | test2 | utf8_unicode_ci |
+--------------+------------+-----------------+
2 rows in set (0.00 sec)
mysql> select table_schema, table_name, column_name, character_set_name, collation_name from information_schema.columns where table_schema = 'SO';
+--------------+------------+-------------+--------------------+-----------------+
| table_schema | table_name | column_name | character_set_name | collation_name |
+--------------+------------+-------------+--------------------+-----------------+
| SO | test1 | col1 | utf8 | utf8_unicode_ci |
| SO | test1 | col2 | utf8 | utf8_unicode_ci |
| SO | test1 | col3 | utf8 | utf8_unicode_ci |
| SO | test2 | col1 | utf8 | utf8_unicode_ci |
| SO | test2 | col2 | utf8 | utf8_unicode_ci |
| SO | test2 | col3 | utf8 | utf8_unicode_ci |
+--------------+------------+-------------+--------------------+-----------------+
6 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
是的。一切看起来都以同样的方式工作。但是,关于仅在与 MySQL 默认值(而不是表默认值)不同时才显示的假设又如何呢?让我们test1回到原来的样子。
mysql> ALTER TABLE test1 CONVERT TO CHARACTER SET latin1 COLLATE latin1_swedish_ci;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test1;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test1 | CREATE TABLE `test1` (
`col1` mediumtext,
`col2` mediumtext,
`col3` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1
+-------+-----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
看起来就像我们刚开始的时候一样。现在为了证明它是 MySQL 默认值而不仅仅是数据库默认值,让我们设置数据库的默认值。
mysql> Alter database SO default character set utf8 collate utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show create table test1;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test1 | CREATE TABLE `test1` (
`col1` mediumtext,
`col2` mediumtext,
`col3` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1
+-------+-----------------+
1 row in set (0.00 sec)
mysql> show create table test2;
+-------+-----------------+
| Table | Create Table
+-------+-----------------+
| test2 | CREATE TABLE `test2` (
`col1` text COLLATE utf8_unicode_ci,
`col2` text COLLATE utf8_unicode_ci,
`col3` text COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------+-----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,test1仍然是我们刚开始时的样子,并且show create table不受数据库默认值的影响。