TUTORIAL INDEX

MySQL CREATE DATABASE

|

What is the CREATE DATABASE Statement in MySQL and Why Do We Use It?

When you want to create a new database in MySQL, you type the CREATE DATABASE statement. The CREATE DATABASE statement is used to create a new database where you can store tables and manage data.


Syntax:

CREATE DATABASE database_name;

Example:

CREATE DATABASE PersonDB;

Output:

If the database is created successfully, MySQL displays the following message:

Query OK, 1 row affected

If the database already exists, MySQL displays the following error message:

ERROR 1007 (HY000):
Can't create database 'PersonDB';
database exists

How the CREATE DATABASE Statement Works Internally


When you want to create a database in MySQL, you type the CREATE DATABASE statement. After pressing the Enter key, the CREATE DATABASE statement is sent to the MySQL server. The server checks whether the database name already exists. If it does not exist, MySQL creates a new database with the given name. If the database already exists, MySQL displays an error message.

Remember that the CREATE DATABASE statement only creates a database. It does not create tables or insert any data inside the database. After creating a database, you can select it using the USE statement and create tables inside it.

Flowchart:

👤 User
   |
   ▼
⌨️ CREATE DATABASE database_name;
   |
   ▼
🖥️ MySQL Server
   |
   ▼
🔍 Check Database Already Exists
   |
 ┌───────┴───────┐
 ▼               ▼
✅ No        ❌ Yes
 |               |
 ▼               ▼
📁 Create   ⚠️ Error
 Database        Message
 |
 ▼
✅ Database Created

Precautions While Using CREATE DATABASE Statement:

  1. Before using the CREATE DATABASE statement, you should know the correct database name and its spelling.
  2. Before creating a database, make sure that the database name does not already exist on the MySQL server.
  3. You should choose a meaningful database name according to your project requirements.
  4. Before creating tables, always select the required database using the USE statement.

How to Use the CREATE DATABASE Statement in MySQL: Step-by-Step Guide

If you use the CREATE DATABASE statement in MySQL, you first need to connect to the MySQL server. After connecting, you use the CREATE DATABASE statement to create a new database. The complete process is as follows:

Step 1: You connect to the MySQL server by using the MySQL login command.

mysql -u username -p

Step 2: You create a new database by typing the CREATE DATABASE statement.

CREATE DATABASE database_name;

Step 3: You select the created database by using the USE statement.

USE database_name;

Step 4: After selecting the database, you can create tables and execute SQL statements on that database.

Note:

  • The CREATE DATABASE statement creates a new database in MySQL.
  • The CREATE DATABASE statement does not create tables or store data inside the database.
  • A database name should be unique on the MySQL server.

Don't Confuse CREATE DATABASE with CREATE TABLE Statement

Many beginners get confused between the CREATE DATABASE statement and the CREATE TABLE statement because both are used to create objects in MySQL. However, they perform different tasks. The CREATE DATABASE statement creates a new database, whereas the CREATE TABLE statement creates a new table inside an existing database.

Therefore, when you want to create a new database, use the CREATE DATABASE statement. After creating a database, use the CREATE TABLE statement to create tables inside that database.

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner