×

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 WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

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

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

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

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

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.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

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

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.

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.

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 UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 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 Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.