gpt4 book ai didi

mysql - 将列排序规则转换为表/数据库默认值

转载 作者:可可西里 更新时间:2023-11-01 06:33:34 24 4
gpt4 key购买 nike

我看到的每一篇关于 SO 的帖子都建议运行以下 SQL:

ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8  COLLATE utf8_unicode_ci;

这个问题,除非我弄错了,否则它明确指定了列排序规则,所以当你对数据库进行 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,

我的问题是.. 如果不这样做,就没有办法将列排序规则转换为表或数据库默认值吗?

例如,我的表格可能如下所示:

  `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,

我想要的是将所有列转换为 utf8_unicode_ci(表/数据库默认值),但没有将每一列显式设置为该排序规则,这样当我 mysqldump 转换后的表时,它看起来像这样:

  `address` varchar(150) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(2) DEFAULT NULL,
`zipcode` varchar(10) DEFAULT NULL,

在定义默认字符集和排序规则的表创建语句末尾有一行:ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

最佳答案

如果您的表或列与 MySQL 默认值不同,在我的例子中是 latin1_sweedish_ci,那么它将打印出与该列的排序规则。请参阅以下证明这一点的实验。

要设置默认字符集,请参阅 this post .

首先,让我们创建一个包含两个表的数据库。一张表指定了字符集和排序规则。

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;

现在检查 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)

我们看到 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)

无论我们是否指定,这些列似乎都具有特定的字符集和排序规则。让我们将 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)

现在他们都将排序规则放在 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)

看起来都差不多。但是当我们向两个表中添加一个额外的列时会发生什么?

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)

在这两种情况下,他们都从表中选取排序规则。因此,不必担心稍后添加的列会出现问题。让我们再检查一次 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)

是的。一切看起来都在以相同的方式工作。但是如果它与 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)

看起来就像我们开始时一样。现在要证明它是 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)

如您所见,test1 仍然是我们刚开始时的样子,show create table 不受数据库默认值的影响。

关于mysql - 将列排序规则转换为表/数据库默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com