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
creates a database with the given name. CREATE DATABASE
创建具有给定名称的数据库。To use this statement, you need the 要使用此语句,您需要数据库的CREATE
privilege for the database. CREATE
权限。CREATE SCHEMA
is a synonym for CREATE DATABASE
.CREATE SCHEMA
是CREATE DATABASE
的同义词。
An error occurs if the database exists and you did not specify 如果数据库存在,并且未指定IF NOT EXISTS
.IF NOT EXISTS
,则会发生错误。
不允许在具有活动CREATE DATABASE
is not permitted within a session that has an active LOCK TABLES
statement.LOCK TABLES
语句的会话中创建数据库。
Each 每个create_option
specifies a database characteristic. Database characteristics are stored in the data dictionary.create_option
指定一个数据库特征。数据库特征存储在数据字典中。
The CHARACTER SET
option specifies the default database character set. CHARACTER SET
选项指定默认的数据库字符集。The COLLATE
option specifies the default database collation. COLLATE
选项指定默认的数据库排序规则。For information about character set and collation names, see Chapter 10, Character Sets, Collations, Unicode.有关字符集和排序规则名称的信息,请参阅第10章,“字符集,排序规则,Unicode”。
To see the available character sets and collations, use the the 要查看可用的字符集和排序规则,请分别使用SHOW CHARACTER SET
and SHOW COLLATION
statements, respectively. SHOW CHARACTER SET
和SHOW COLLATION
语句。See Section 13.7.7.3, “SHOW CHARACTER SET Statement”, and Section 13.7.7.4, “SHOW COLLATION Statement”.请参阅第13.7.7.3节,“SHOW CHARACTER SET语句”和第13.7.7.4节,“SHOW COLLATION语句”。
The MySQL 8.0.16中引入的ENCRYPTION
option, introduced in MySQL 8.0.16, defines the default database encryption, which is inherited by tables created in the database. ENCRYPTION
选项定义了默认的数据库加密,由数据库中创建的表继承。The permitted values are 允许的值为'Y'
(encryption enabled) and 'N'
(encryption disabled). 'Y'
(已启用加密)和'N'
(已禁用加密)。If the 如果未指定ENCRYPTION
option is not specified, the value of the default_table_encryption
system variable defines the default database encryption. ENCRYPTION
选项,则系统变量default_table_encryption
的值定义默认数据库加密。If the 如果启用了系统变量table_encryption_privilege_check
system variable is enabled, the TABLE_ENCRYPTION_ADMIN
privilege is required to specify a default encryption setting that differs from the default_table_encryption
setting. table_encryption_privilege_check
,则需要TABLE_ENCRYPTION_ADMIN
权限来指定与default_table_encryption
设置不同的默认加密设置。For more information, see Defining an Encryption Default for Schemas and General Tablespaces.有关更多信息,请参阅为架构和常规表空间定义加密默认值。
A database in MySQL is implemented as a directory containing files that correspond to tables in the database. MySQL中的数据库实现为包含与数据库中的表相对应的文件的目录。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. CREATE DATABASE
语句只在MySQL数据目录下创建一个目录。Rules for permissible database names are given in Section 9.2, “Schema Object Names”. 第9.2节,“模式对象名称”中给出了允许的数据库名称规则。If a database name contains special characters, the name for the database directory contains encoded versions of those characters as described in Section 9.2.4, “Mapping of Identifiers to File Names”.如果数据库名称包含特殊字符,则数据库目录的名称包含这些字符的编码版本,如第9.2.4节,“标识符到文件名的映射”所述。
Creating a database directory by manually creating a directory under the data directory (for example, with mkdir) is unsupported in MySQL 8.0.MySQL 8.0不支持通过在数据目录下手动创建目录(例如,使用mkdir)来创建数据库目录。
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 has no limit on the number of databases. MySQL对数据库的数量没有限制。The underlying file system may have a limit on the number of directories.基础文件系统可能对目录的数量有限制。
You can also use the mysqladmin program to create databases. 您还可以使用mysqladmin程序创建数据库。See Section 4.5.2, “mysqladmin — A MySQL Server Administration Program”.请参阅第4.5.2节,“mysqladmin-一个MySQL服务器管理程序”。