CREATE DATABASE
In this page:
CREATE DATABASE [IF NOT EXISTS] database_name;
Basic Database Creation
CREATE DATABASE followed by a name allocates a new logical container for your tables; the name must be unique on that server instance, and MySQL rejects duplicates outright without a safeguard.
Example: Basic Database Creation
CREATE DATABASE shop;
Safely Creating Databases
Adding IF NOT EXISTS after CREATE DATABASE makes the statement idempotent — running it again on a database that already exists produces a warning instead of a hard error, which is essential in setup scripts run more than once.
Example: Safely Creating Databases
CREATE DATABASE IF NOT EXISTS shop;
Creating with Character Sets
Specifying CHARACTER SET utf8mb4 when creating a database ensures it can store the full Unicode range, including emoji and many non-Latin scripts — the older utf8 alias in MySQL is actually a 3-byte subset that can't.
Example: Creating with Character Sets
CREATE DATABASE shop CHARACTER SET utf8mb4;
Specifying Collation Rules
Collation rules determine whether text comparisons and sorting are case-sensitive or accent-sensitive; picking the wrong collation for your application's language can cause search results to feel subtly wrong later.
Example: Specifying Collation Rules
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Viewing the Created Database
Querying INFORMATION_SCHEMA or running SHOW DATABASES afterward confirms your new database actually exists with the character set and collation you intended, catching typos before you build tables on top of it.
Example: Viewing the Created Database
SHOW DATABASES;
- Running
CREATE DATABASE shop;twice, which fails withdatabase existsunless you addIF NOT EXISTS. - Forgetting to run
USE shop;afterwards, so the followingCREATE TABLEgoes into the wrong database or fails. - Creating a database without
utf8mb4, so emoji and some characters cannot be stored in older setups.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: