Skip to content

MySQL Tutorial Working with Databases

CREATE DATABASE

Lesson 27 of 308 2 min read By Deepak Paswan Updated Oct 5, 2026

A database is the container that holds your tables. You create one with CREATE DATABASE.

query.sql
SQL
CREATE DATABASE shop;
SHOW DATABASES LIKE 'shop';
Output
OUTPUT
+-----------------+
| Database (shop) |
+-----------------+
| shop            |
+-----------------+
1 row in set

Syntax

query.sql
SQL
CREATE DATABASE [IF NOT EXISTS] db_name
    [DEFAULT] CHARACTER SET charset_name
    [DEFAULT] COLLATE collation_name;

In MySQL, CREATE SCHEMA is a synonym: a “schema” and a “database” are the same thing here (see Schema vs Database).

IF NOT EXISTS

Creating a database that already exists is an error:

query.sql
SQL
CREATE DATABASE shop;
Output
OUTPUT
ERROR 1007 (HY000): Can't create database 'shop'; database exists

Add IF NOT EXISTS to make a script safe to run twice. MySQL then gives a note instead of an error.

query.sql
SQL
CREATE DATABASE IF NOT EXISTS shop;
SHOW WARNINGS;
Output
OUTPUT
+-------+------+-----------------------------------------------+
| Level | Code | Message                                       |
+-------+------+-----------------------------------------------+
| Note  | 1007 | Can't create database 'shop'; database exists |
+-------+------+-----------------------------------------------+
1 row in set

Choose the character set and collation

query.sql
SQL
CREATE DATABASE blog
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_0900_ai_ci;
SHOW CREATE DATABASE blog;
Output
OUTPUT
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                                                |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| blog     | CREATE DATABASE `blog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
1 row in set
  • utf8mb4 is the character set to use for new work. It stores every Unicode character, including emoji.
  • utf8mb4_0900_ai_ci is the default collation in MySQL 8: accent-insensitive and case-insensitive.

If you leave them out, the database uses the server defaults, which are utf8mb4 and utf8mb4_0900_ai_ci in MySQL 8. Being explicit in your scripts makes them behave the same on every server, because hosting providers sometimes change the defaults. Character Sets and Collations explains the choices.

info
MariaDB
MariaDB does not know utf8mb4_0900_ai_ci. A script that names it will fail there. utf8mb4_unicode_ci or utf8mb4_general_ci work on both.

What /!80016 DEFAULT ENCRYPTION='N' / means

In the output you saw a comment that starts with /*!80016. It is an executable comment: MySQL 8.0.16 and later run the part inside it (the DEFAULT ENCRYPTION option) and other servers ignore it.

Names and rules

  • Use lowercase letters, digits and underscores: shop, blog_2026. The name length limit is 64 characters.
  • The name becomes a folder on disk, so the operating system’s case rules apply (SQL Case Sensitivity).
  • Avoid reserved words and spaces.

Permissions

You need the CREATE privilege. A normal application user usually should not have it. Create databases from a deployment or admin account.

query.sql
SQL
CREATE DATABASE IF NOT EXISTS app_v1 CHARACTER SET utf8mb4;
lightbulb
One database per application
Give each application its own database, and its own user (Application Database Users). Then one project cannot read or break another’s data.