Learn how to create a database using SQL CREATE database statement in this tutorial for MySQL and Other RDBMS databases. The SQL
CREATE DATABASE statement is used by many developers to create the database on MySQL server for storing data.
If you have installed MySQL database, you can use this statement as a beginner who wants to create a database.
CREATE DATABASE databasename
Creating a database uses the statement
CREATE DATABASE with database name after this or at the end. Use the meaningful name for the database to relate it to your project name.
SQL CREATE Database Statement Example
If database does not exist
CREATE DATABASE mydb;
The above example creates the database with the output as given above.
In the above example, I am creating a database name “mydb”. Always create a database with a name related to your project name.
If you are creating a database for bakery shop, then you can use database name ‘bakeryshop’, ‘mybakeryshop’ or ‘bakery_shop’. The name matches with the application name. With the name of the database, you can easily find it from the list of a database in MySQL database or other database server.
If the database already exists
If the database ‘mydb’ already exists in the database, the SQL query gives an error message as given below.
CREATE DATABASE mydb;
CREATE DATABASE mydb
MySQL said: Documentation
#1007 – Can’t create database ‘mydb’; database exists
To avoid any error message occurring from the SQL
CREATE DATABASE statement, you can use below given statement.
Check If Database Exist Before SQL CREATE Database
Sometimes, you forget a database that you have already created. When you run a query for creating a database, SQL gives you error message that ‘database already exists.’.
SQL also provide a statement to add after the
CREATE DATABASE statement which is
IF NOT EXISTS. This checks if the database exists it will not create any database and does not give any error message. In addition to this, it also checks if the database not exists, it will create the database.
CREATE DATABASE IF NOT EXISTS mydb;
The above statement will not give any error message in MySQL database.
USE statement after creating a database
USE statement is required when you have many databases and you want to create a table in a specified database using SQL. You need to specify first that you want to use this database to run this query. In that case, you can use this statement.
In the above example, you have specified that you want to use the database ‘mydb’.
You must also read.