CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_option] ...
create_option: [DEFAULT] {
CHARACTER SET [=] charset_name
| COLLATE [=] collation_name
| ENCRYPTION [=] {'Y' | 'N'}
}
CREATE DATABASE带有名字的数据库。要使用该语句,你需要数据库的 CREATE 权限。CREATE
SCHEMA 是 CREATE
DATABASE“。CREATE DATABASE creates a database
with the given name. To use this statement, you need the
CREATE privilege for the database.
CREATE
SCHEMA is a synonym for CREATE
DATABASE.
如果数据库存在且你未指定 IF NOT EXISTS。An error occurs if the database exists and you did not specify
IF NOT EXISTS.
CREATE DATABASE 不允许在具有活跃 LOCK
TABLES 语句。CREATE DATABASE is not permitted
within a session that has an active LOCK
TABLES statement.
每个create_option指定数据库特征。数据库特征存储在数据字典中。Each create_option specifies a database
characteristic. Database characteristics are stored in the data
dictionary.
CHARACTER SET指定默认数据库字符集。COLLATE选项指定了默认的数据库排序。有关字符集和排序名称的信息,请参见 第 12 章,字符集、排序、Unicode。TheCHARACTER SEToption specifies the default database character set. TheCOLLATEoption specifies the default database collation. For information about character set and collation names, see Chapter 12, Character Sets, Collations, Unicode.要查看可用的字符集和校对,分别使用
SHOW CHARACTER SETSHOW COLLATION语句。参见 第 15.7.7.4 节,“SHOW CHARACTER SET Statement”,以及 第 15.7.7.5 节,“SHOW COLLATION Statement”。To see the available character sets and collations, use the theSHOW CHARACTER SETandSHOW COLLATIONstatements, respectively. See Section 15.7.7.4, “SHOW CHARACTER SET Statement”, and Section 15.7.7.5, “SHOW COLLATION Statement”.ENCRYPTION选项定义了默认的数据库加密设置,该设置会被数据库中创建的表继承。允许的值为'Y'(启用加密)和'N'(禁用加密)。如果未指定ENCRYPTION选项,则由default_table_encryption系统变量的值定义默认的数据库加密。如果启用了table_encryption_privilege_check系统变量,则需要具有TABLE_ENCRYPTION_ADMIN权限才能指定与default_table_encryption设置不同的默认加密设置。有关详细信息,请参见 为模式和通用表空间定义加密默认值。TheENCRYPTIONoption defines the default database encryption, which is inherited by tables created in the database. The permitted values are'Y'(encryption enabled) and'N'(encryption disabled). If theENCRYPTIONoption is not specified, the value of thedefault_table_encryptionsystem variable defines the default database encryption. If thetable_encryption_privilege_checksystem variable is enabled, theTABLE_ENCRYPTION_ADMINprivilege is required to specify a default encryption setting that differs from thedefault_table_encryptionsetting. For more information, see Defining an Encryption Default for Schemas and General Tablespaces.
MySQL 中的数据库实现为包含与数据库中表对应的文件的目录。由于在数据库初始创建时没有表,因此 CREATE DATABASE 语句仅在 MySQL 数据目录下创建一个目录。允许的数据库名称规则见 第 11.2 节,“模式对象名称”。如果数据库名称包含特殊字符,则数据库目录的名称包含这些字符的编码版本,如 第 11.2.4 节,“标识符映射到文件名” 中所述。A database in MySQL is implemented as a directory containing files
that correspond to tables in the database. Because there are no
tables in a database when it is initially created, the
CREATE DATABASE statement creates
only a directory under the MySQL data directory. Rules for
permissible database names are given in
Section 11.2, “Schema Object Names”. If a database name contains special
characters, the name for the database directory contains encoded
versions of those characters as described in
Section 11.2.4, “Mapping of Identifiers to File Names”.
在数据目录下通过手动创建目录(例如使用 mkdir)来创建数据库目录在 MySQL 8.4 中不受支持。Creating a database directory by manually creating a directory under the data directory (for example, with mkdir) is unsupported in MySQL 8.4.
当你创建数据库时,让服务器管理目录及其中文件。直接操作数据库目录和文件可能会导致不一致和意外结果。When you create a database, let the server manage the directory and the files in it. Manipulating database directories and files directly can cause inconsistencies and unexpected results.
MySQL 对数据库的数量没有限制。底层文件系统可能对目录的数量有限制。MySQL has no limit on the number of databases. The underlying file system may have a limit on the number of directories.
你也可以使用 mysqladmin 程序来创建数据库。参见 第 6.5.2 节,“mysqladmin — 一个 MySQL 服务器管理程序”。You can also use the mysqladmin program to create databases. See Section 6.5.2, “mysqladmin — A MySQL Server Administration Program”.