SQL Query To Create Database In MySQL

By bhagwatchouhan
SQL Query To Create Database In MySQL

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 mentioned below. If you are remotely logged in to the database, you will also need the CREATE privilege 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.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan