The SQL CREATE DATABASE statement is used by a developer to create a database.
In this tutorial you will learn how to create database in a relational database management system like, MySQL, SQL Server, etc. using SQL.Before doing anything with the data we must need to create a database first. We're assuming that you already have a MySQL, or SQL Server available for your use, as well as you've all the necessary privileges, if not please check out the getting started guide.
The basic syntax for creating a database can be given with:
The following SQL statement creates a database named demo:
Creating a database does not select it for use. So, before moving further we must need to select the target database with the USE statement. For example, the USE demo; command sets the demo database as target database for all future commands.
Let's create a database in MySQL using the command-line tool.
Step 1: Invoke the MySQL command-line tool
To invoke the MySQL command line, we've to log in to the MySQL server first. To log in as root user, type the following command in terminal and press enter. You will be asked for your password. Enter your password and press enter, if it is correct the mysql> prompt will appear, via which you will be able to issue SQL statements and view the results.
Step 2: Creating a MySQL Database
Now, execute the following command to create the database named demo.
If the database created successfully you'll see the output something like this:
If you try to create a database that is already exists you'll get an error message. To avoid this in MySQL you can use an optional clause IF NOT EXISTS
as follow:
Type the following command and press enter. You will see the output "Database changed". Now our demo database is selected as default database for all future operations.