← Back to Chapters

MySQL CREATE DATABASE

?️ MySQL CREATE DATABASE

MySQL

⚡ Quick Overview

The CREATE DATABASE statement is used to create a new database in MySQL. A database groups related tables, views, and other objects so your data stays organized.

  • Executed once for each new database you need.
  • Requires a unique database name on the MySQL server.
  • Often the very first step before creating tables.

? Key Concepts

  • Database: A logical container that holds tables and other objects.
  • Database name: Must be unique on the server (e.g., EmployeeDB).
  • CREATE DATABASE: Command used to create a fresh, empty database.
  • IF NOT EXISTS: Optional safety check to avoid errors if the DB already exists.
  • Naming convention: Prefer lowercase + underscores (e.g., employee_db) for portability and readability.

? Syntax & Theory

Basic syntax to create a new database:

? View Basic Syntax
-- Syntax to create a new database
CREATE DATABASE database_name;

Safer syntax using IF NOT EXISTS:

? View Safe Syntax
-- Create database only if it does not already exist
CREATE DATABASE IF NOT EXISTS database_name;

In both cases, database_name is the identifier you choose for the database. Using IF NOT EXISTS prevents an error if someone already created it earlier.

? Code Examples

Example 1: Create a simple employee database

? View Code Example 1
-- Create a new database for employee records
CREATE DATABASE EmployeeDB;

Example 2: Use naming convention with lowercase + underscore

? View Code Example 2
-- Create a database using a clean, portable name
CREATE DATABASE employee_db;

Example 3: Create only if it does not already exist

? View Code Example 3
-- Avoid error by checking existence first
CREATE DATABASE IF NOT EXISTS employee_db;

? What Happens When You Run These Commands?

  • MySQL checks whether a database with the given name exists.
  • If it does not exist, MySQL creates a new, empty database container.
  • The database will then appear in the list returned by SHOW DATABASES;.

For example, after running CREATE DATABASE EmployeeDB;, you can confirm its creation:

? View Verification Query
-- List all databases to verify creation
SHOW DATABASES;

You should now see EmployeeDB (or employee_db) in the result set, confirming that the database was successfully created.

? Common Use Cases

  • Setting up a fresh project (e.g., HR system, e-commerce app, school portal).
  • Creating separate databases for development, testing, and production.
  • Organizing different clients’ data into separate databases in multi-tenant systems.

✅ Tips & Best Practices

  • Use meaningful, descriptive names that reflect the domain (e.g., inventory_db).
  • Stick to lowercase letters, digits, and underscores to avoid portability issues.
  • Use CREATE DATABASE IF NOT EXISTS in setup scripts to avoid errors on re-runs.
  • Always verify the database creation using SHOW DATABASES; after running the command.
  • Plan your schema (tables and relationships) before creating too many databases unnecessarily.

? Try It Yourself

  • Create a new database named school_db using CREATE DATABASE.
  • Re-run your script using CREATE DATABASE IF NOT EXISTS school_db; and observe that it does not fail.
  • List all databases using SHOW DATABASES; and confirm that school_db exists.
  • After creating the database, switch to it using USE school_db; and create at least one table.