Before you can store any data in MySQL, you need a database to hold it. A database is a named container that groups all related tables together. The CREATE DATABASE statement is used to create a new, empty database on the MySQL server.
The basic syntax for creating a database is:
CREATE DATABASE database_name;
Replace database_name with the name you want to give your database. The name must be unique on the server — no two databases can share the same name.
The following statement creates a new database called school:
CREATE DATABASE school;
Once executed successfully, MySQL will respond with:
Query OK, 1 row affected (0.01 sec)
This means the database was created without any errors. The database is now empty — no tables or data yet. You will add those in the next steps of this tutorial series.
If you try to create a database that already exists, MySQL will return an error:
-- This will throw an error if 'school' already exists
CREATE DATABASE school;
-- ERROR 1007 (HY000): Can't create database 'school'; database exists
To avoid this error — especially useful in scripts that may run more than once — use the IF NOT EXISTS clause:
CREATE DATABASE IF NOT EXISTS school;
With this version, if the database already exists, MySQL simply skips the creation and shows a warning instead of an error. Your script continues running without interruption.
IF NOT EXISTS when writing database setup scripts. It makes your scripts safe to run multiple times without causing errors.
When creating a database, you can specify how text characters are stored (character set) and how they are compared and sorted (collation). If you skip these options, MySQL uses its server-wide defaults.
For most modern applications — especially those handling multiple languages or emojis — the recommended settings are:
CREATE DATABASE school
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
| Option | Value | What it means |
|---|---|---|
CHARACTER SET |
utf8mb4 |
Supports all Unicode characters including emojis (4-byte UTF-8) |
COLLATE |
utf8mb4_unicode_ci |
Case-insensitive sorting and comparison using Unicode rules |
utf8 character set only supports 3-byte characters and cannot store emojis or certain rare Unicode symbols. utf8mb4 is the correct full Unicode support and should be your default choice for any new database.
When choosing a name for your database, follow these rules:
_)my_app)select, table)ecommerce_db, blog, hospital)After creating a database, you can confirm it exists by running:
SHOW DATABASES;
Your new database will appear in the list. We will cover this command in detail in the next lesson.
CREATE DATABASE name; creates a new empty database.IF NOT EXISTS to prevent errors if the database already exists.CHARACTER SET utf8mb4 and COLLATE utf8mb4_unicode_ci for full Unicode support.