This topic describes the requirements and examples for creating a database.
About databases
In MySQL mode of OceanBase Database, a database is a collection of database objects. After you connect to a MySQL tenant of OceanBase Database, you can create a database in the MySQL tenant to store and manage its data.
Note
In Oracle mode of OceanBase Database, a schema is the collection of database objects owned by a user. Each user has a default schema when the user is created, and the schema name is the username. Unlike MySQL tenants, an Oracle tenant of OceanBase Database can store and manage its data without the need for you to create a schema.
Preparations
Before you create a database, make sure that:
You have logged on to a MySQL tenant of OceanBase Database. For more information about how to connect to OceanBase Database, see Connect to OceanBase Database.
Note
You can query the
oceanbase.DBA_OB_TENANTSview in thesystenant to confirm the mode of the tenant to which you have logged on.You have the
CREATEprivilege. For more information about how to view your privileges, see View user privileges. If you do not have the required privileges, contact the administrator. For more information, see Modify user privileges.
Create a database
Use the CREATE DATABASE statement to create a database. Then, complete the creation of the database based on the suggestions and examples in this topic.
Suggestions on database creation
Do not directly use the default database in the system. We recommend that you use an SQL statement to create your own database.
In OceanBase Database, the name of each database must be globally unique.
Do not use reserved keywords as the database name. For more information about the reserved keywords in OceanBase Database in MySQL mode, see Reserved keywords.
To adapt to long-term business development, the character set of the database must be able to store most characters. We recommend that you use the
UTF8character set when you create a database. For more information about the character sets supported by OceanBase Database, see Database-level character sets.
Examples
Create a database and specify its character set
Create a database named test2 and specify the character set to UTF8.
obclient [test]> CREATE DATABASE test2 DEFAULT CHARACTER SET UTF8;
Query OK, 1 row affected
obclient [test]> SHOW CREATE DATABASE test2;
+----------+-------------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` DEFAULT CHARACTER SET = utf8mb4 REPLICA_NUM = 1 |
+----------+-------------------------------------------------------------------------+
1 row in set
Create a database that supports read and write operations
Create a database named test3 that supports read and write operations.
obclient [test]> CREATE DATABASE test3 READ WRITE;
Query OK, 1 row affected
obclient [mwx]> SHOW CREATE DATABASE test3;
+----------+-------------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------------+
| test3 | CREATE DATABASE `test3` DEFAULT CHARACTER SET = utf8mb4 REPLICA_NUM = 1 |
+----------+-------------------------------------------------------------------------+
1 row in set
Create a read-only database
Create a read-only database named test4.
obclient [test]> CREATE DATABASE test4 READ ONLY;
Query OK, 1 row affected
obclient [test]> SHOW CREATE DATABASE test4;
+----------+-----------------------------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------------------------+
| test4 | CREATE DATABASE `test4` DEFAULT CHARACTER SET = utf8mb4 REPLICA_NUM = 1 READ ONLY |
+----------+-----------------------------------------------------------------------------------+
1 row in set