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
| Parameter | Description |
| database_name | Specify the name of the database to modify. If this parameter is omitted, the current default database is modified. |
| CHARSET charset_name | Specify the target character set. |
| COLLATE collation_name | Specifies the collation. |
| LOCALITY | Specifies 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:
Important
|
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.
The following result is returned:SHOW CREATE DATABASE db1;+----------+------------------------------------------------------------------------+ | DATABASE | CREATE DATABASE | +----------+------------------------------------------------------------------------+ | db1 | CREATE DATABASE `db1` /* LOCALITY = "dn=polardbx-storage-0-master" */ | +----------+------------------------------------------------------------------------+ 1 row in set
该文章对您有帮助吗?