CREATE DATABASE

Overview

The CREATE DATABASE statement is used to create a new database. A database is a container for storing tables, views, indexes, and other database objects.

Creating a database is typically the first step in setting up a new database system for your application.

Basic Syntax

CREATE DATABASE Syntax
CREATE DATABASE database_name;

Examples

Example 1: Simple Database Creation

Create a new database
CREATE DATABASE company_db;

Example 2: Database with Character Set

Specify character set (MySQL)
CREATE DATABASE company_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

Example 3: Check if Database Exists

Avoid errors if database exists
-- MySQL/SQL Server
CREATE DATABASE IF NOT EXISTS company_db;

-- PostgreSQL
CREATE DATABASE company_db;

Database Naming Conventions

Best practices for naming databases:

Good vs Bad Names
✅ Good:
company_db
ecommerce_store
student_management

❌ Bad:
Database        (reserved word)
my-db          (hyphens may cause issues)
123database    (starts with number)

Selecting a Database

After creating a database, you need to select (use) it before creating tables:

USE Statement
-- Create database
CREATE DATABASE company_db;

-- Select/use the database
USE company_db;

-- Now you can create tables in this database
CREATE TABLE employees (...);

Database-Specific Syntax

Different database systems have varying syntax for CREATE DATABASE:

MySQL

MySQL CREATE DATABASE
CREATE DATABASE IF NOT EXISTS company_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

PostgreSQL

PostgreSQL CREATE DATABASE
CREATE DATABASE company_db
WITH ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8';

SQL Server

SQL Server CREATE DATABASE
CREATE DATABASE company_db
ON (NAME = 'company_db', FILENAME = 'C:\Data\company_db.mdf')
LOG ON (NAME = 'company_db_log', FILENAME = 'C:\Data\company_db_log.ldf');

Viewing Databases

To see all databases in your system:

List all databases
-- MySQL
SHOW DATABASES;

-- PostgreSQL
\l

-- SQL Server
SELECT name FROM sys.databases;

Permissions Required

Creating a database typically requires administrative privileges:

Important Considerations

Common Errors

Best Practices

Next Steps

After creating a database, learn about creating tables with CREATE TABLE, or learn about removing databases with DROP DATABASE.