CREATE DATABASE

更新时间:
复制 MD 格式

The CREATE DATABASE statement creates a database. You can specify default properties for the database, such as its character set and collation.

Syntax

create_database_stmt:
    CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] database_name [database_option_list]

database_option_list:
    database_option [database_option ...]

database_option:
    [DEFAULT] {CHARACTER SET | CHARSET} [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name
  |  LOCALITY = locality_option
  | [{PARTITION_MODE|MODE} = partition_mode_option]

locality_option:
    'dn=storage_inst_id_list'

storage_inst_id_list:
    storage_inst_id[,storage_inst_id_list]

partition_mode_option:
     {'auto'|'partitioning'}
    |{'drds'|'sharding'}

Parameters

ParameterDescription
database_nameSpecify the name of the database to modify. If this parameter is omitted, the current default database is modified.
CHARSET charset_nameSpecify the target character set.
COLLATE collation_nameSpecifies the collation.
LOCALITYSpecifies the storage location of the database when it is created.
Note After you specify the storage location of a database using the LOCALITY syntax, you cannot change the location.
MODE
Specifies the partitioning mode for the logical database. The following modes are supported:
  • auto/partitioning: AUTO mode
    • If you do not manually specify a partition definition, the database is automatically partitioned by the primary key.
    • You can create tables in the database only using the new CREATE TABLE (AUTO mode) syntax, such as `partition by hash/Range/List`.
  • drds/sharding: DRDS mode. You can create tables in the database only using the CREATE TABLE (DRDS mode) syntax, such as `dbpartition by/tbpartition by`.
Important
  • If you do not specify a mode when you create the database, `drds/sharding` is used by default.
  • The partitioning mode of a database cannot be changed after it is specified.

Examples

  • Create a database named `test` and set its character set to UTF-8.
    CREATE DATABASE test default CHARACTER SET=UTF8 mode=auto;
    Query OK, 1 row affected (3.36 sec)                   
  • Create a database in an instance and store it on the polardbx-storage-0-master node.
    CREATE DATABASE db1 LOCALITY='dn=polardbx-storage-0-master';
    Note
    • If you do not specify a storage location when you create a database, the system distributes the database evenly across all storage nodes by default.
    • The storage location of sharded tables in a database is the same as that of the database. This provides data isolation for the sharded tables.
    After the database is created, you can run the following statement to view its storage location information.
    SHOW CREATE DATABASE db1;
    The following result is returned:
    +----------+------------------------------------------------------------------------+
    | DATABASE | CREATE DATABASE                                                        |
    +----------+------------------------------------------------------------------------+
    | db1      | CREATE DATABASE `db1` /* LOCALITY = "dn=polardbx-storage-0-master" */  |
    +----------+------------------------------------------------------------------------+
    1 row in set