以下示例演示了 MySQL 如何确定默认字符集和排序规则值。The following examples show how MySQL determines default character set and collation values.
示例 1:表和列的定义Example 1: Table and Column Definition
CREATE TABLE t1
(
c1 CHAR(10) CHARACTER SET latin1 COLLATE latin1_german1_ci
) DEFAULT CHARACTER SET latin2 COLLATE latin2_bin;
这里我们有一列使用 latin1 字符集和 latin1_german1_ci 校对规则。定义是明确的,所以很直接。请注意,将 latin1 列存储在 latin2 表中没有问题。Here we have a column with a latin1 character
set and a latin1_german1_ci collation. The
definition is explicit, so that is straightforward. Notice that
there is no problem with storing a latin1
column in a latin2 table.
示例 2:表和列的定义Example 2: Table and Column Definition
CREATE TABLE t1
(
c1 CHAR(10) CHARACTER SET latin1
) DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci;
这次我们有一个 latin1 字符集并且使用默认排序规则的列。虽然看起来很自然,但默认排序规则并不是从表级继承的。相反,由于 latin1 的默认排序规则始终是 latin1_swedish_ci,所以列 c1 的排序规则是 latin1_swedish_ci(而不是 latin1_danish_ci)。This time we have a column with a latin1
character set and a default collation. Although it might seem
natural, the default collation is not taken from the table
level. Instead, because the default collation for
latin1 is always
latin1_swedish_ci, column
c1 has a collation of
latin1_swedish_ci (not
latin1_danish_ci).
示例 3:表和列定义Example 3: Table and Column Definition
CREATE TABLE t1
(
c1 CHAR(10)
) DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci;
我们有一个使用默认字符集和默认排序规则的列。在这种情况下,MySQL 会检查表级以确定列的字符集和排序规则。因此,列 c1 的字符集是 latin1,其排序规则是 latin1_danish_ci。We have a column with a default character set and a default
collation. In this circumstance, MySQL checks the table level to
determine the column character set and collation. Consequently,
the character set for column c1 is
latin1 and its collation is
latin1_danish_ci.
例 4:数据库、表和列定义Example 4: Database, Table, and Column Definition
CREATE DATABASE d1
DEFAULT CHARACTER SET latin2 COLLATE latin2_czech_cs;
USE d1;
CREATE TABLE t1
(
c1 CHAR(10)
);
我们创建一个未指定字符集和排序规则的列。我们也没有在表级别指定字符集和排序规则。在这种情况下,MySQL 会检查数据库级别以确定表的设置,这些设置随后会成为列的设置。因此,列 c1 的字符集为 latin2,其排序规则为 latin2_czech_cs。We create a column without specifying its character set and
collation. We're also not specifying a character set and a
collation at the table level. In this circumstance, MySQL checks
the database level to determine the table settings, which
thereafter become the column settings.) Consequently, the
character set for column c1 is
latin2 and its collation is
latin2_czech_cs.