Create a Database in MySQL
Introduction
Before storing any data in MySQL, the first step is to create a database.
A database acts as a container that holds tables and other objects, where all your data will be stored and managed.
In this article, you will learn how to create a database in MySQL using simple SQL commands.
What is CREATE DATABASE
The CREATE DATABASE statement is used to create a new database in MySQL.
Once a database is created, you can:
Create tables inside it.
Insert and manage data.
Organise your application data properly.
Basic Syntax
The syntax to create a database is very simple:
CREATE DATABASE database_name; Example
CREATE DATABASE School; This command creates a new database named School.
Verify Database Creation
To check whether the database has been created successfully, you can use:
SHOW DATABASES; This will display a list of all databases, including the newly created one.
CREATE DATABASE with IF NOT EXISTS
Sometimes, you may try to create a database that already exists. This can cause an error.
To avoid this, MySQL provides a safer option:
CREATE DATABASE IF NOT EXISTS School; This ensures that:
The database is created only if it does not already exist.
No error is thrown if the database already exists.
Choosing a Database Name
While creating a database, follow these best practices:
Use meaningful names (e.g.,
school_db,ecommerce_db)Avoid spaces in names.
Use lowercase for consistency.
Keep names short but descriptive.
Example Scenario
Suppose you are building a student management system.
You would first create a database:
CREATE DATABASE student_management; Then you can create tables like:
Students
Courses
Teachers
inside this database.
Common Mistakes
Using spaces in database names
Forgetting to check if the database already exists
Not verifying creation using
SHOW DATABASES.
Key Points to Remember
CREATE DATABASEis used to create a new databaseA database is required before creating tables.
Use
IF NOT EXISTSto avoid errors.Always verify using
SHOW DATABASES