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:

  1. 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:

  1. mysql
  2. -u root \
  3. -h {host} \
  4. -P {port} \
  5. -p {password} \
  6. -e "CREATE DATABASE IF NOT EXISTS bookshop;"

View databases

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

For example:

  1. mysql
  2. -u root \
  3. -h {host} \
  4. -P {port} \
  5. -p {password} \
  6. -e "SHOW DATABASES;"

The following is an example output:

  1. +--------------------+
  2. | Database |
  3. +--------------------+
  4. | INFORMATION_SCHEMA |
  5. | PERFORMANCE_SCHEMA |
  6. | bookshop |
  7. | mysql |
  8. | test |
  9. +--------------------+

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.