×

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 Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 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.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 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.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.