Lesson 10 of 60 – CREATE DATABASE
17%

CREATE DATABASE in MySQL

The CREATE DATABASE statement is used to create a new database in MySQL. A database provides a container where tables and other database objects can be stored.

Note: Before creating tables for an application, you normally create a database and then select it using the USE command.

1. What is CREATE DATABASE?

CREATE DATABASE is a MySQL statement used to create a new database.

CREATE DATABASE school;

This creates a database named school.

2. Basic Syntax

The basic syntax is:

CREATE DATABASE database_name;

Replace database_name with the name you want to use.

3. Creating a School Database

For a school management system, you could create:

CREATE DATABASE school;

The database can later contain tables such as students, teachers, courses, and fees.

4. Creating a Library Database

For a library management system, you could create:

CREATE DATABASE library;

The database could contain books, students, issues, and returns tables.

5. Database Name

The database name identifies the database on the MySQL server.

Examples:

school
library
company
hospital
shop

Use meaningful names that describe the purpose of the database.

6. Semicolon

A semicolon normally terminates a SQL statement.

CREATE DATABASE school;

The semicolon indicates the end of the statement in the MySQL command-line client and many SQL tools.

7. Creating Multiple Databases

A MySQL server can contain multiple databases.

CREATE DATABASE school;
CREATE DATABASE library;
CREATE DATABASE company;

Each statement creates a separate database.

8. SHOW DATABASES

After creating a database, use SHOW DATABASES to see databases accessible to the current user.

SHOW DATABASES;

9. CREATE DATABASE IF NOT EXISTS

Use IF NOT EXISTS when you want MySQL to avoid an error if the database already exists.

CREATE DATABASE IF NOT EXISTS school;

If school already exists, MySQL will not create another database with the same name.

10. Why Use IF NOT EXISTS?

This option is useful in installation scripts and application setup scripts that may be executed more than once.

CREATE DATABASE IF NOT EXISTS student_management;

It helps make the script safer when the database may already exist.

11. Error When Database Already Exists

If you run a normal CREATE DATABASE statement for a database that already exists, MySQL can report an error.

CREATE DATABASE school;

Using IF NOT EXISTS can avoid this specific situation.

12. Selecting the New Database

Creating a database does not automatically make it the current database.

Use the USE statement:

CREATE DATABASE school;

USE school;

13. Checking the Current Database

After using a database, you can check the current database with:

SELECT DATABASE();

For example, after USE school;, the result should identify school as the current database.

14. Creating a Table After CREATE DATABASE

After creating and selecting a database, you can create tables inside it.

CREATE DATABASE school;

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

15. Creating a Student Database

A student management application could use:

CREATE DATABASE student_management;

After selecting it, tables such as students, courses, attendance, and fees can be created.

16. Creating a Company Database

A company application could use:

CREATE DATABASE company_db;

It could contain employees, departments, projects, and salary tables.

17. Creating a Hospital Database

A hospital management application could use:

CREATE DATABASE hospital_db;

Tables could later be created for patients, doctors, appointments, and billing.

18. Creating an E-Commerce Database

An online shopping application could use:

CREATE DATABASE ecommerce_db;

Possible tables include products, customers, orders, and payments.

19. Database Creation Workflow

1. Connect to MySQL
2. Create the database
3. Show databases
4. Select the database
5. Create tables
6. Insert data
7. Query the data

20. CREATE DATABASE in MySQL Workbench

You can execute the CREATE DATABASE statement in MySQL Workbench's SQL Editor.

CREATE DATABASE school;

After execution, refresh the Schemas panel to see the database.

21. CREATE DATABASE in Command Line

The same SQL statement can be executed from the MySQL command-line client.

mysql> CREATE DATABASE school;

SQL works independently of whether you use Workbench or the command line to send it to the MySQL server.

22. Database Permissions

A MySQL user must have appropriate privileges to create databases.

If the user does not have the required permission, MySQL may reject the CREATE DATABASE operation.

23. Meaningful Database Names

Use names that clearly describe the application or project.

Good examples include:

school_db
library_db
student_management
inventory_db

Clear naming makes projects easier to understand and maintain.

24. Avoid Confusing Names

Avoid unnecessarily confusing database names.

For example, instead of:

db1
database123
testabc

prefer names that describe the purpose of the database.

25. CREATE DATABASE with IF NOT EXISTS

A practical database setup statement can be:

CREATE DATABASE IF NOT EXISTS school_db;

This is commonly useful when preparing an application database through an installation or setup script.

26. Complete Example with Tables

CREATE DATABASE IF NOT EXISTS school;

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    course VARCHAR(100)
);

CREATE TABLE courses (
    id INT PRIMARY KEY,
    course_name VARCHAR(100)
);

Here, the database is created first and then two tables are created inside it.

27. Checking the Database

You can verify that the database exists with:

SHOW DATABASES;

Then select it:

USE school;

Finally, check its tables:

SHOW TABLES;

28. Common Errors

Common issues while creating a database include:

  • Database already exists
  • Insufficient privileges
  • Invalid database name
  • SQL syntax error
  • MySQL Server is not running
  • Connection problem

29. Complete CREATE DATABASE Example

CREATE DATABASE IF NOT EXISTS student_management;

SHOW DATABASES;

USE student_management;

SELECT DATABASE();

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    mobile VARCHAR(15)
);

SHOW TABLES;

This example creates a database, checks available databases, selects the new database, verifies it, creates a table, and displays the tables.

30. Practical Project Example

Suppose you are developing a library management system. First create the database:

CREATE DATABASE IF NOT EXISTS library_db;

USE library_db;

CREATE TABLE books (
    id INT PRIMARY KEY,
    title VARCHAR(150),
    author VARCHAR(100)
);

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    mobile VARCHAR(15)
);

SHOW TABLES;

The library_db database now provides a container for the tables required by the library application.

📌 Key Points

  • CREATE DATABASE creates a new MySQL database.
  • The basic syntax is CREATE DATABASE database_name;
  • IF NOT EXISTS helps avoid an error when the database already exists.
  • SHOW DATABASES displays accessible databases.
  • Creating a database does not automatically select it.
  • USE database_name selects a database.
  • SELECT DATABASE() checks the currently selected database.
  • Tables can be created after selecting the database.
  • The user needs appropriate privileges to create databases.
  • Meaningful database names make applications easier to manage.

🧠 Quick Quiz

Question: Which statement is used to create a new database in MySQL?