CREATE DATABASE

This statement creates a new database in TiDB. The MySQL terminology for ‘database’ most closely maps to a schema in the SQL standard.

Synopsis

CreateDatabaseStmt

CREATE DATABASE - 图1

IfNotExists

CREATE DATABASE - 图2

DBName

CREATE DATABASE - 图3

DatabaseOptionListOpt

CREATE DATABASE - 图4

DatabaseOptionList

CREATE DATABASE - 图5

DatabaseOption

CREATE DATABASE - 图6

PlacementPolicyOption

CREATE DATABASE - 图7

  1. CreateDatabaseStmt ::=
  2. 'CREATE' 'DATABASE' IfNotExists DBName DatabaseOptionListOpt
  3. IfNotExists ::=
  4. ( 'IF' 'NOT' 'EXISTS' )?
  5. DBName ::=
  6. Identifier
  7. DatabaseOptionListOpt ::=
  8. DatabaseOptionList?
  9. DatabaseOptionList ::=
  10. DatabaseOption ( ','? DatabaseOption )*
  11. DatabaseOption ::=
  12. DefaultKwdOpt ( CharsetKw '='? CharsetName | 'COLLATE' '='? CollationName | 'ENCRYPTION' '='? EncryptionOpt )
  13. | DefaultKwdOpt PlacementPolicyOption
  14. PlacementPolicyOption ::=
  15. "PLACEMENT" "POLICY" EqOpt PolicyName
  16. | "PLACEMENT" "POLICY" (EqOpt | "SET") "DEFAULT"

Syntax

The CREATE DATABASE statement is used to create a database, and to specify the default properties of the database, such as the default character set and collation. CREATE SCHEMA is a synonym for CREATE DATABASE.

  1. CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
  2. [create_specification] ...
  3. create_specification:
  4. [DEFAULT] CHARACTER SET [=] charset_name
  5. | [DEFAULT] COLLATE [=] collation_name

If you create an existing database and does not specify IF NOT EXISTS, an error is displayed.

The create_specification option is used to specify the specific CHARACTER SET and COLLATE in the database. Currently, TiDB only supports some of the character sets and collations. For details, see Character Set and Collation Support.

Examples

  1. mysql> CREATE DATABASE mynewdatabase;
  2. Query OK, 0 rows affected (0.09 sec)
  3. mysql> USE mynewdatabase;
  4. Database changed
  5. mysql> CREATE TABLE t1 (a int);
  6. Query OK, 0 rows affected (0.11 sec)
  7. mysql> SHOW TABLES;
  8. +-------------------------+
  9. | Tables_in_mynewdatabase |
  10. +-------------------------+
  11. | t1 |
  12. +-------------------------+
  13. 1 row in set (0.00 sec)

MySQL compatibility

The CREATE DATABASE statement in TiDB is fully compatible with MySQL. If you find any compatibility differences, report a bug.

See also