Create a Database

This document describes how to create a database using SQL and various programming languages and lists the rules of database creation. In this document, the Bookshop application is taken as an example to walk you through the steps of database creation.

Before you start

Before creating a database, do the following:

What is database

Database objects in TiDB contain tables, views, sequences, and other objects.

Create databases

To create a database, you can use the CREATE DATABASE statement.

For example, to create a database named bookshop if it does not exist, use the following statement:

CREATE DATABASE IF NOT EXISTS `bookshop`;

For more information and examples of the CREATE DATABASE statement, see the CREATE DATABASE document.

To execute the library build statement as the root user, run the following command:

mysql -u root \ -h {host} \ -P {port} \ -p {password} \ -e "CREATE DATABASE IF NOT EXISTS bookshop;"

View databases

To view the databases in a cluster, use the SHOW DATABASES statement.

For example:

mysql -u root \ -h {host} \ -P {port} \ -p {password} \ -e "SHOW DATABASES;"

The following is an example output:

+--------------------+ | Database | +--------------------+ | INFORMATION_SCHEMA | | PERFORMANCE_SCHEMA | | bookshop | | mysql | | test | +--------------------+

Rules in database creation

  • Follow the Database Naming Conventions and name your database meaningfully.
  • TiDB comes with a default database named test. However, it is not recommended that you use it in a production environment if you do not have to. You can create your own database using the CREATE DATABASE statement and change the current database using the USE {databasename}; statement in a SQL session.
  • Use the root user to create objects such as database, roles, and users. Grant only the necessary privileges to roles and users.
  • As a best practice, it is recommended that you use a MySQL command-line client or a MySQL GUI client instead of a driver or ORM to execute database schema changes.

Next step

After creating a database, you can add tables to it. For more information, see Create a Table.

Need help?

Ask questions on TiDB Community, or create a support ticket.