13.1.12 CREATE DATABASE Statement语句

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 SCHEMACREATE 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指定一个数据库特征。数据库特征存储在数据字典中。

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服务器管理程序”