×

How to create a database in SQL?

How to create a database in SQL

  • It is very necessary to create a database in order to store the data into the database.
  • Database name must always be unique.
  • SQL does not allow to create a database with the same name which already exists in the server.
  • To ensure the uniqueness while creating a new database, the database administer must be aware of the already existing databases into the server.
  • The SHOW DATABASES command is used in SQL to list all the databases which are present in server.

Example:

           We will check all the available databases.

 mysql> SHOW DATABASES;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | demo               |
 | mysql              |
 | performance_schema |
 | test               |
 +--------------------+
 5 rows in set (0.05 sec) 
create a database in SQL

In the above example, all the databases which are present in server including the system databases(information_schema, performance_schema, mysql) and user created databases(demo, test) are displayed.

  • Once the database administrator is familiar with the names of already existing databases which are present in SQL server, now the database administrator can create a database with any name considering the output of SHOW DATABASES command.

Syntax for creating a database in SQL:

CREATE DATABASE DATABASENAME;

Example: We will create a new database with the name “Exampledb”.

mysql> CREATE DATABASE Exampledb;
create a database in SQL

       A new database with the name “exampledb” is successfully created in the   server.

  • To ensure that the database is successfully created, we will again use the SHOW DATABASES command.

Example:

We will display all the available databases to check if the “exampledb” is created or not.

mysql> SHOW DATABASES;

Output:

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | demo               |
 | exampledb         |
 | mysql              |
 | performance_schema |
 | test               |
 +--------------------+ 
create a database in SQL

Now, we can see the newly created database with the name “exampledb” is also listed.

  • Alternative to CREATE DATABASE command is CREATE SCHEMA COMMAND. Both queries performs the same task of database creation.

Syntax:

CREATE SCHEMA DATABASENAME;

Example: We will use an alternate query to create a database with the name “testDB”.

mysql> CREATE SCHEMA testDB;
create a database in SQL

Example: We will display all the available databases to check if the “testdb” is created or not.

mysql> SHOW DATABASES;

Output

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | demo               |
 | exampledb         |
 | mysql              |
 | performance_schema |
 | test               |
 | testdb             |
 +--------------------+
 7 rows in set (0.05 sec) 
create a database in SQL
  • One can also review the already created database using the below command:
SHOW CREATE DATABASE DATABASE_NAME;

Example:

 We will review the already created database named as “exampledb”.
 mysql> SHOW CREATE DATABASE exampledb; 

Output:

 +------------+-----------------------------------------------------------------------+
 | Database   | Create Database                                                       |
 +------------+-----------------------------------------------------------------------+
 | exampledb | CREATE DATABASE ` exampledb ` /*!40100 DEFAULT CHARACTER SET latin1 */ |
 +------------+-----------------------------------------------------------------------+
 1 row in set (0.00 sec) 
create a database in SQL

Here, the command which was used to create the database named “exampledb” is displayed along with the character set. Since, the character set was not specified during that database creation, the database is created with the default character set i.e., latin1.

  • To store the data into a particular database, one needs to tell the server which specific database the administrator wants to use.

Syntax:

USE DATABASE_NAME;

Example:

To operate on a specific database, the user needs to tell the server that he wants to perform further queries on “exampledb”.

mysql> USE exampledb;

Output:

Database changed
create a database in SQL

Since, we have used the database named as “exampledb”, all the further queries will now be operated on this particular database.

Parameters of CREATE statement

One can also improvise the CREATE DATABASE query by adding parameters and specifications to it.

  1. IF NOT EXISTS

There can be multiple databases in a single MySQL server. One may try to create a database which is already present within the MySQL server. So, in that case using “IF NOT EXISTS” parameter with the CREATE DATABASE query serves the purpose. Prior to the creation of a new database, it instructs the server to check if the database already exists with the specified name.

Syntax:

CREATE DATABASE IF NOT EXISTS DATABASENAME;

Example:

In order to create a new database “demodb”, we will first display all the available databases and then create “demodb” if a database with that name does not already exist.

mysql> SHOW DATABASES;

Output:

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | demo               |
 | exampledb        |
 | mysql              |
 | performance_schema |
 | test               |
 | testdb             |
 +--------------------+
 7 rows in set (0.05 sec) 
create a database in SQL
 mysql> CREATE DATABASE IF NOT EXISTS demodb;
 Query OK, 1 row affected (0.00 sec) 
create a database in SQL

Earlier there was no database with the name “demodb”. So, now the database with name “demodb” will be created. If we try to create a database with the existing name without the use of IF NOT EXISTS, then in that case SQL will throw an error.

Example:

Now, we will display all the available databases to ensure that the database “demodb” is created.

mysql> SHOW DATABASES;

Output:

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | demo               |
 | demodb             |
 | exampledb        |
 | mysql              |
 | performance_schema |
 | test               |
 | testdb             |
 +--------------------+
 8 rows in set (0.00 sec) 
create a database in SQL

Here, database with the name “demodb” is created.

  • Collation and Character Set

Collation is a set of rules which are useful for comparison. One can store the SQL data in different language other than English. To store the data in some other language, you need to select the character set for that particular language. Different levels of character set includes server, database, table, and column. Once the character set is chosen, then only the rules of collation can be selected.

Example:

We will create a new database with name “sample” whose character set is “latin1” and collation rule is “latin1_swedish_ci”.

mysql> CREATE DATABASE IF NOT EXISTS sample CHARACTER SET latin1 COLLATE latin1_swedish_ci;
create a database in SQL

Here, “sample” database is created with latin1 as its character set and latin1_swedish_ci as its collation rule.


Related Topics

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

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

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.

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

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.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.