SQL Query To Create Database In MySQL

SQL Query To Create Database In MySQL

Learn how to create a database in MySQL using the SQL query CREATE DATABASE.

May 13, 2019

This is the first tutorial of the series Learn Basic SQL Queries Using MySQL. In this tutorial, we will discuss SQL queries to create a database in MySQL.

Simple Query

The simplest query to create a database is as mentioned below. In case you are remotely logged in to the database, you will also need CREATE privilege in order to create a database.

# To do - Create Database
# Query - CREATE DATABASE <database name>

# It might throw error in case database already exist
CREATE DATABASE enterprise;

OR

# Good to go
CREATE DATABASE IF NOT EXISTS enterprise;

Similar to CREATE DATABASE, you can also use CREATE SCHEMA as shown below.

# To do - Create Database
# Query - CREATE SCHEMA <database name>

# It might throw error in case database already exist
CREATE SCHEMA enterprise;

OR

# Good to go
CREATE SCHEMA IF NOT EXISTS enterprise;

Advanced Query

You can also specify advanced options including character set and collation as shown below.

# UTF-8
CREATE SCHEMA enterprise DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

# UTF-8 MB4 - Since MySQL 8
CREATE SCHEMA enterprise DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Alter Database

We can change the overall characteristics of a MySQL database using the ALTER DATABASE command as shown below. In case you are remotely logged in to the database, you will also need ALTER privilege in order to modify an existing database.

# Alter Schema - Change character set and collation
ALTER SCHEMA enterprise DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This is how we can create a database in MySQL using the SQL query.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS