×

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples.

The first step for storing structured records in the database is creating the databases.

We create a database to store data in the database. We also create a database to create tables, indexes, and views into the database.

We use the CREATE DATABASE query to create a database followed by the database name. The database name must be attractive, easy to remember, and, most importantly, unique. The database with the same name cannot be created on the server.

The database user must know which database name already exists in the database server.

Use the SHOW DATABASES query to find out the already residing database name in the SQL Server.

Before creating a new database, let's check which database already present in my SQL Server

SHOW DATABASES;

The output of the above query is as follows:

Database
cricket
demo
ids
information_schema
mysql
performance_schema
phpadmin
registration
student
student_1
WordPress
SQL Create Database

Before creating a new database, we must learn about the syntax of the create database query.

The syntax of the CREATE DATABASE is as follows:

CREATE DATABASE Database_Name;

Now, we are ready to create new databases in the SQL Server.

Example 1: Write a query to create a new database named javatpoint

CREATE DATABASE javatpoint;

In the above query, we created a new database named javatpoint.

We will execute the SHOW DATABASES query to check whether the javatpoint database is successfully created or not is as follows:

SHOW DATABASES;

The output of the above query is as follows:

Database
cricket
demo
ids
information_schema
javatpoint
mysql
performance_schema
phpadmin
registration
student
student_1
WordPress
SQL Create Database

Example 2: Write a query to create a new database named Mumbai

CREATE DATABASE Mumbai;

In the above query, we created a new database named Mumbai.

We will execute the SHOW DATABASES query to check whether the Mumbai database is successfully created or not is as follows:

SHOW DATABASES;

The output of the above query is as follows:

Database
cricket
demo
demodb
ids
information_schema
javatpoint
Mumbai
mysql
performance_schema
phpadmin
registration
Student
student_1
WordPress
SQL Create Database

CREATE SCHEMA

CREATE SCHEMA is an alternative option or command to create the database. Both query task is the same to create a database in the SQL Server.

The syntax of the CREATE SCHEMA is as follows:

CREATE SCHEMA Schema_Name;

Example 1: Write a query to create a new schema named testDB

CREATE DATABASE testDB;

In the above query, we created a new database named testDB.

We will execute the SHOW DATABASES query to check whether the testDB database is successfully created or not is as follows:

SHOW DATABASES;

The output of the above query is as follows:

Database
Cricket
Demo
Demodb
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
phpadmin
registration
student
student_1
testdb
WordPress
SQL Create Database

Example 2: Write a query to create a new schema named Employee

CREATE DATABASE Employee;

In the above query, we created a new database named Employee.

We will execute the SHOW DATABASES query to check whether the Employee database is successfully created or not is as follows:

SHOW DATABASES;

The output of the above query is as follows:

Database
Cricket
Demo
Demodb
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
phpadmin
registration
student
student_1
testdb
WordPress
SQL Create Database

SHOW DATABASES

The SHOW DATABASES is the one way to see the already existing database. Another way is to SHOW CREATE DATABASE command. But, in the SHOW DATABASES, all the database's name is listed, and in the SHOW CREATE DATABASE query, only the database that wants to look is displayed with the following character set of the database name.

Example 1: Execute a query to show the employee database.

SHOW CREATE DATABASE Employee;

In the above query, we show the employee database with the character set information of the employee database.

The output of the above query is as follows:

DatabaseCreate Database
EmployeeCREATE DATABASE `employee` /*!40100 DEFAULT CHARACTER SET utf8mb4 */
SQL Create Database

Since when we created the database employee, we didn't specify the character set with the database. So, the default character set is displayed in the employee database.

We created a database without adding arguments or parameters in the above examples.

IF NOT EXISTS

IF NOT EXISTS is one of the parameters which is used while creating the database in the SQL Server. Suppose there are several databases in the SQL Server, and we don't know whether the database is creating resides in the SQL Server. The purpose of the IF NOT EXISTS parameter is to check whether the database we are going to create is already residing or not in the SQL Server.

The syntax of the CREATE DATABASE with the IF NOT EXISTS parameter is as follows:

CREATE DATABASE IF NOT EXISTS DataBase_Name;

Example 1: Write a query to create a new database with the IF NOT EXISTS parameter.

CREATE DATABASE IF NOT EXISTS World;

In the above query, we execute the create database query with the IF NOT EXISTS parameter database name World. If the database is already residing on the server, then the warning message will be displayed or will create the database successfully.

The output of the above query is as follows:

Database
Cricket
Demo
Demodb
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
phpadmin
registration
student
student_1
testdb
WordPress
world
SQL Create Database

There was no database named world on the server. So, created the Database name World successfully. Now, in the last example, we will create a database which already resides in the database and check for the result.

Example 2: Write a query to create a new database with the IF NOT EXISTS parameter.

CREATE DATABASE IF NOT EXISTS Employee;

In the above query, we execute the create database query with the IF NOT EXISTS parameter database name Employee. If the database is already residing on the server, a warning message will display and create the database successfully.

The output of the above query is as follows:

SQL Create Database

As we can see in the output 1 warning is shown, this is because database name employee is already residing in the server.


Related Topics

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.