Lesson 50 of 158 – Create MySQL Database
50%

Create MySQL Database

Before creating tables and building our PHP REST API, we need a MySQL database to store the application data. In this lesson, we will create the student_api database that will be used by our Student Management REST API project.

Note: We will use MySQL for storing the data and phpMyAdmin or the MySQL command line to create the database.

1. What is a Database?

A database is an organized collection of data. Applications use databases to store information so that it can be created, retrieved, updated, and deleted.

Our Student Management API will store student information inside a MySQL database.

React Native
      ↓
PHP REST API
      ↓
MySQL Database

2. Why Do We Need a Database?

A mobile application may work with a large amount of information. That data needs to be stored somewhere permanently.

For our project, the database will store:

  • Student names
  • Email addresses
  • Mobile numbers
  • Courses
  • Other student information

3. What is MySQL?

MySQL is a relational database management system. It stores data inside tables made up of rows and columns.

Our PHP REST API will communicate with MySQL to perform database operations.

4. What Database Will We Create?

For our Student Management API, we will create a database named:

student_api

This database will contain the tables required by our REST API.

5. Open phpMyAdmin

If you are using a local development environment such as XAMPP, start Apache and MySQL.

Then open phpMyAdmin from your local server environment.

phpMyAdmin provides a graphical interface for managing MySQL databases.

6. Create a New Database

In phpMyAdmin, open the database creation section and enter the name:

student_api

Then create the database.

7. Database Name

The database name should match the name used by our PHP connection.

$dbname = "student_api";

If the PHP code uses a different database name, the connection will not point to the database we created.

8. Create Database Using SQL

A MySQL database can also be created using SQL.

CREATE DATABASE student_api;

This SQL statement creates a database named student_api.

9. Use the Database

After creating the database, we can select it using:

USE student_api;

This tells MySQL that subsequent table operations should be performed inside the selected database.

10. Check the Database

You can check available databases using:

SHOW DATABASES;

The result should contain:

student_api

11. Check the Selected Database

After selecting the database, you can check which database is currently selected.

SELECT DATABASE();

The result should show:

student_api

12. Database and Tables

A database can contain multiple tables.

student_api
│
├── students
├── users
└── courses

We will create the required tables in the next database lessons.

13. Student Table

The main table for our project will store student information.

students

id
name
email
mobile
course

The table will be created after the database is ready.

14. Database Structure

Think of the database as a container for related tables.

Database
    ↓
Tables
    ↓
Rows
    ↓
Columns

Our student_api database will contain the data required by the REST API.

15. Database with PHP

PHP needs the database name when creating the PDO connection.

$host = "localhost";
$dbname = "student_api";
$username = "root";
$password = "";

These values are examples for a local development environment and may be different on another server.

16. PDO Connection DSN

The database name becomes part of the PDO Data Source Name.

$dsn = "mysql:host=localhost;dbname=student_api;charset=utf8mb4";

This tells PDO which MySQL database should be used.

17. Database Connection Flow

PHP
 ↓
PDO
 ↓
MySQL Server
 ↓
student_api
 ↓
Tables

The PHP REST API will use this connection to work with the database.

18. Create Database from SQL

The complete SQL command for creating our database is:

CREATE DATABASE student_api;

After executing this command, MySQL creates the database.

19. Create Database If It Does Not Exist

To avoid an error when the database already exists, you can use:

CREATE DATABASE IF NOT EXISTS student_api;

This creates the database only when it does not already exist.

20. Database Name Case

Use a consistent database name throughout the project.

student_api

The same name should be used in the PHP configuration.

$dbname = "student_api";

21. Verify the Database in phpMyAdmin

After creating the database, look at the database list in phpMyAdmin.

You should see:

student_api

Click the database to open it. Initially, it may not contain any tables.

22. Empty Database

Immediately after creation, the database may be empty.

student_api
    ↓
No Tables Yet

This is normal. Tables will be created in the next step of the project.

23. Database and REST API

The REST API will use the database to store and retrieve information.

React Native
      ↓
REST API
      ↓
PHP
      ↓
student_api
      ↓
students table

24. Database Creation Using SQL Tool

You can use the SQL section of phpMyAdmin or another MySQL client to execute the database creation statement.

CREATE DATABASE student_api;

After execution, refresh the database list to verify the result.

25. Common Database Creation Errors

  • Using an invalid database name
  • Database already exists
  • MySQL server is not running
  • Insufficient database privileges
  • SQL syntax errors
  • Connecting to the wrong MySQL server

Check the MySQL service and SQL statement when database creation fails.

26. Database Already Exists

If the database already exists, running the normal CREATE DATABASE statement may produce an error.

You can use:

CREATE DATABASE IF NOT EXISTS student_api;

This prevents an error caused by the database already existing.

27. Check Database from MySQL

You can verify the database using SQL commands.

SHOW DATABASES;

USE student_api;

SHOW TABLES;

At this stage, SHOW TABLES may return an empty result because we have not created any tables yet.

28. Database Project Flow

Create MySQL Database
        ↓
Select Database
        ↓
Create Tables
        ↓
Insert Data
        ↓
PHP REST API
        ↓
Postman
        ↓
React Native

This is the overall development sequence for our project.

29. Final Database Setup

At the end of this lesson, our project should have the following database:

Database:
student_api

Current Status:
Database created
Tables: Not created yet

The next lesson will focus on creating the required database tables.

30. Create MySQL Database Summary

The first database step in our PHP REST API project is creating the student_api database. This database will contain the tables used by our Student Management API. PHP will later connect to this database using PDO.

MySQL
  ↓
student_api
  ↓
Database Tables
  ↓
PHP REST API
  ↓
React Native

📌 Key Points

  • A database stores application data.
  • Our project uses MySQL.
  • Our database name is student_api.
  • The database can be created using phpMyAdmin.
  • The database can also be created using SQL.
  • Use CREATE DATABASE student_api; to create the database.
  • CREATE DATABASE IF NOT EXISTS avoids an error when the database already exists.
  • USE student_api; selects the database.
  • SHOW DATABASES; displays available databases.
  • SHOW TABLES; displays tables in the selected database.
  • The PHP PDO connection uses the database name.
  • Tables will be created in the next step.

🧠 Quick Quiz

Question: What is the name of the MySQL database we are creating for the Student Management REST API?