每个数据库都有一个数据库字符集和数据库排序规则。CREATE DATABASE 和 ALTER DATABASE 语句有可选的子句用于指定数据库字符集和排序规则:Every database has a database character set and a database
collation. The CREATE DATABASE
and ALTER DATABASE statements
have optional clauses for specifying the database character set
and collation:
CREATE DATABASEdb_name[[DEFAULT] CHARACTER SETcharset_name] [[DEFAULT] COLLATEcollation_name] ALTER DATABASEdb_name[[DEFAULT] CHARACTER SETcharset_name] [[DEFAULT] COLLATEcollation_name]
关键字 SCHEMA 可以代替 DATABASE 使用。The keyword SCHEMA can be used instead of
DATABASE.
CHARACTER SET 和 COLLATE 子句可以使同一个 MySQL 服务器上创建具有不同字符集和排序规则的数据库成为可能。The CHARACTER SET and
COLLATE clauses make it possible to create
databases with different character sets and collations on the
same MySQL server.
数据库选项存储在数据字典中,可以通过检查信息模式 SCHEMATA 表来查看。Database options are stored in the data dictionary and can be
examined by checking the Information Schema
SCHEMATA table.
示例:Example:
CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;
MySQL 以下列方式选择数据库字符集和数据库排序规则:MySQL chooses the database character set and database collation in the following manner:
如果同时指定了
CHARACTER SET charset_name和COLLATE collation_name,将使用字符集charset_name和校对规则collation_name。If bothCHARACTER SETandcharset_nameCOLLATEare specified, character setcollation_namecharset_nameand collationcollation_nameare used.如果指定了
CHARACTER SET charset_name而未指定COLLATE,则使用字符集charset_name及其默认排序规则。要查看每个字符集的默认排序规则,请使用SHOW CHARACTER SET语句或查询INFORMATION_SCHEMACHARACTER_SETS表。IfCHARACTER SETis specified withoutcharset_nameCOLLATE, character setcharset_nameand its default collation are used. To see the default collation for each character set, use theSHOW CHARACTER SETstatement or query theINFORMATION_SCHEMACHARACTER_SETStable.如果指定了
COLLATE collation_name而没有指定CHARACTER SET,则使用与collation_name相关联的字符集和排序规则collation_name。IfCOLLATEis specified withoutcollation_nameCHARACTER SET, the character set associated withcollation_nameand collationcollation_nameare used.否则(既未指定
CHARACTER SET也未指定COLLATE),则使用服务器字符集和服务器排序规则。Otherwise (neitherCHARACTER SETnorCOLLATEis specified), the server character set and server collation are used.
默认数据库的字符集和排序规则可以从 character_set_database 和 collation_database 系统变量的值中确定。每当默认数据库更改时,服务器会设置这些变量。如果没有默认数据库,这些变量的值与相应的服务器级系统变量 character_set_server 和 collation_server 相同。The character set and collation for the default database can be
determined from the values of the
character_set_database and
collation_database system
variables. The server sets these variables whenever the default
database changes. If there is no default database, the variables
have the same value as the corresponding server-level system
variables, character_set_server
and collation_server.
要查看给定数据库的默认字符集和排序规则,请使用以下语句:To see the default character set and collation for a given database, use these statements:
USE db_name;
SELECT @@character_set_database, @@collation_database;
或者,要在不更改默认数据库的情况下显示这些值:Alternatively, to display the values without changing the default database:
SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'db_name';
数据库字符集和排序规则会影响服务器操作的以下方面:The database character set and collation affect these aspects of server operation:
对于
CREATE TABLE语句,如果未指定表的字符集和校对规则,则使用数据库字符集和校对规则作为表定义的默认值。要覆盖此设置,请提供明确的CHARACTER SET和COLLATE表选项。ForCREATE TABLEstatements, the database character set and collation are used as default values for table definitions if the table character set and collation are not specified. To override this, provide explicitCHARACTER SETandCOLLATEtable options.对于未包含
CHARACTER SET子句的LOAD DATA语句,服务器使用由character_set_database系统变量指示的字符集来解释文件中的信息。要覆盖此设置,请提供明确的CHARACTER SET子句。ForLOAD DATAstatements that include noCHARACTER SETclause, the server uses the character set indicated by thecharacter_set_databasesystem variable to interpret the information in the file. To override this, provide an explicitCHARACTER SETclause.对于存储例程(存储过程和函数),在创建例程时生效的数据库字符集和排序规则将用于那些声明中未包含
CHARACTER SET或COLLATE属性的字符数据参数。要覆盖此设置,请明确提供CHARACTER SET和COLLATE。For stored routines (procedures and functions), the database character set and collation in effect at routine creation time are used as the character set and collation of character data parameters for which the declaration includes noCHARACTER SETor aCOLLATEattribute. To override this, provideCHARACTER SETandCOLLATEexplicitly.