MySQL CREATE DATABASE
Everything you need to know about creating MySQL databases.
What is a Database in MySQL?
Think of a MySQL database as a filing cabinet. Each cabinet (database) contains drawers (tables), and each drawer holds folders (rows) with documents (data). Before you can store anything, you need the cabinet.
In MySQL, a database is a container that holds related tables, views, stored procedures, and other objects. You'll typically have separate databases for different applications or environments (development, staging, production).
Basic Syntax
Creating a database is one of the simplest operations in MySQL:
That's it. MySQL will create an empty database ready for tables.
Character Set and Collation
Here's where most tutorials fall short. Character set and collation matter—especially if your app will ever handle non-English text, emojis, or special characters.
Character Set
Defines which characters can be stored. Always use utf8mb4 (not utf8—MySQL's utf8 only supports 3 bytes and can't handle all Unicode characters like emojis).
Collation
Defines how characters are compared and sorted. Use utf8mb4_unicode_ci for case-insensitive comparisons that handle international characters correctly.
Why
utf8mb4_unicode_ci? Thecimeans case-insensitive, so 'Apple' equals 'apple' in searches. Theunicodepart means it follows Unicode sorting rules, handling accented characters properly (é, ñ, ü).
Handling Existing Databases
Check If Database Exists First
This prevents errors if the database already exists. Useful in scripts that might run multiple times.
Drop and Recreate (Dangerous!)
Warning: This deletes ALL data in the database permanently. Only use this in development or when you're absolutely certain.
Viewing Your Databases
List all databases on the server:
See details about a specific database:
This shows the exact CREATE statement with character set and collation.
Switching Databases
After creating a database, you need to select it before creating tables:
Or specify the database in your queries:
Naming Best Practices
Good database names are:
- Lowercase: Avoids case-sensitivity issues across operating systems
- Snake_case or single word:
my_app,user_service,analytics - Descriptive: The name should hint at what's inside
- Short but clear: You'll type this a lot
Common Patterns
One Database Per Application
Most web applications use a single database:
Environment-Specific Databases
For development workflows:
Multi-Tenant (Database Per Customer)
Some applications create a database per customer for isolation:
Permissions
After creating a database, you'll typically create a user with access to it:
Quick Reference
| Command | Description |
|---------|-------------|
| CREATE DATABASE name | Create a new database |
| CREATE DATABASE IF NOT EXISTS name | Create only if it doesn't exist |
| DROP DATABASE name | Delete a database (permanent!) |
| SHOW DATABASES | List all databases |
| SHOW CREATE DATABASE name | Show creation statement |
| USE name | Switch to a database |
Summary
Creating a MySQL database is simple, but getting the details right matters:
- Always use
utf8mb4character set for full Unicode support - Use
utf8mb4_unicode_cicollation for case-insensitive, international-friendly comparisons - Use
IF NOT EXISTSin scripts to make them idempotent - Follow naming conventions: lowercase, no spaces, descriptive
With your database created, you're ready to start creating tables.