← Back to MySQL Course | Chapter 2: Databases & Tables | Lesson 1 of 7

CREATE DATABASE

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

sql
CREATE DATABASE shop;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
CREATE DATABASE IF NOT EXISTS shop;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
CREATE DATABASE shop CHARACTER SET utf8mb4;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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

sql
SHOW DATABASES;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.