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

CREATE DATABASE

CREATE DATABASE is like buying a brand new empty cabinet and giving it a name, ready to hold your tables.
Syntax
sql
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

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.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running CREATE DATABASE shop; twice, which fails with database exists unless you add IF NOT EXISTS.
  2. Forgetting to run USE shop; afterwards, so the following CREATE TABLE goes into the wrong database or fails.
  3. 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:

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.