SQL Create Database

In order to store and manipulate data in a database, you need to first create a database. This database subsequently holds tables and data. We are assuming that you already have MySQL or SQL server with you to use. If you don’t have these servers, you can check our tutorial on how to install MySQL database on your computer.

The SQL CREATE DATABASE statement is used to create a SQL database.

Syntax

The basic syntax of the SQL CREATE DATABASE is as follows.

CREATE DATABASE databasename;

The databasename should be unique throughout the RDBMS.

Example

The following statement will create a new SQL DATABASE with the name testDB.

SQL> CREATE DATABASE testDB;

Make sure that you have the admin privilege before creating any database. Once a database is created, you can check it in the list of databases as follows.

SQL> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| celebsradar        |
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| teradatapoint      |
| test               |
| testdb             |
+--------------------+
8 rows in set (0.002 sec)

 

 

Create Database in MySQL

Now let’s check how to create a database in MySQL.

#1. Open MySQL Command Line Client

In order to create a MySQL database, first you have to connect to MySQL server. Open MySQL Command Line Client. It will by default prompt for root password. Please provide the root password you have created during MySQL database installation. Once you give the root password mysql> prompt will come. Here you provide the command to create MySQL databases.

#2. Creating a MySQL database

Now execute the following command to create a database in MySQL.

mysql> create database testDB;

If the database created successfully, it will show following result.

Query OK, 1 row affected (0.23 sec)

The database name should be unique throughout the server. If you are trying to create a database that is already exists you’ll get an error message. To avoid this in MySQL you can use an optional clause IF NOT EXISTS as follow:

mysql> CREATE DATABASE IF NOT EXISTS testDB;

Leave a Comment