SQL CREATE DATABASE Statement

In this tutorial, we'll learn about creating databases in SQL with examples.

Before we can work with database tables, we must create a database first.

The CREATE DATABASE statement is used to create database tables. For example,

CREATE DATABASE my_db;

Here, the SQL command creates a database named my_db.


CREATE DATABASE IF NOT EXISTS

If there is already a database with the same name, SQL will throw an error while creating a database.

In such situations, we can use the CREATE DATABASE IF NOT EXISTS statement to create a database only if there is no existing database with the same name. For example,

CREATE DATABASE IF NOT EXISTS my_db;

Here, the SQL command creates a database named my_db only if there is no existing database with the same name.


List all Databases

There could be multiple databases in a database management system. To show the list of databases, we can run the following statement.

SHOW DATABASES;

Here, the SQL command lists all the available databases in the DBMS.


Switch Databases

We have to work across multiple databases from time to time. To switch between available databases, we can run the following statement.

USE my_db;

This code selects the my_db database and all SQL operations will be performed inside this database.


Recommended Reading

Did you find this article helpful?