Lesson 138 of 158 – Project Database Design
87%

Project Database Design

In this lesson, we will design the MySQL database for our complete Student Management mobile application.

The database will store user authentication information and student records. The PHP REST API will communicate with MySQL using PDO and prepared statements.

Project Goal: Create a clean database structure that can support registration, login, JWT authentication, student CRUD operations, search, pagination, and user management.

1. Database Name

We can create a database named:

student_management

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

2. Creating the Database

CREATE DATABASE student_management;

After creating the database, select it before creating tables.

USE student_management;

3. Main Database Tables

Our project will initially use two main tables:

student_management
│
├── users
│
└── students
  • users stores application users.
  • students stores student records.

4. Users Table

The users table will store the information required for registration and login.

users

id
name
email
password
role
created_at

The password field will contain a secure password hash rather than the original password.

5. Creating the Users Table

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role VARCHAR(30) NOT NULL DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

The email is unique so that two users cannot register using the same email address.

6. Users ID

The id column uniquely identifies each user.

id INT AUTO_INCREMENT PRIMARY KEY

For example:

ID Name Email
1 Rahul rahul@example.com
2 Amit amit@example.com

7. User Email

Email will be used as the login identifier.

email VARCHAR(150) NOT NULL UNIQUE

The UNIQUE constraint prevents duplicate email addresses.

8. Password Storage

Never store a user's plain password in the database.

PHP can create a password hash using:

$hash = password_hash(
    $password,
    PASSWORD_DEFAULT
);

The generated hash is stored in the password column.

9. User Roles

The role field can be used for authorization.

role

Example values:

admin
user

Later, the API can check the user's role before allowing administrative operations.

10. Students Table

The students table will store the student information managed by the mobile application.

students

id
name
email
mobile
course
address
created_at

11. Creating the Students Table

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100),
    address TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

12. Student ID

The id column uniquely identifies each student.

id INT AUTO_INCREMENT PRIMARY KEY

When a new student is inserted, MySQL automatically generates the next ID.

13. Student Name

The student's name is required.

name VARCHAR(100) NOT NULL

The PHP API should also validate the name before inserting it into the database.

14. Student Email

The student's email can be stored in the email column.

email VARCHAR(150)

The API can validate the email format before saving it.

15. Student Mobile Number

A mobile number can be stored using a VARCHAR field.

mobile VARCHAR(20)

Phone numbers should generally be stored as text because they are identifiers rather than mathematical values.

16. Student Course

The course field stores the course or program in which the student is enrolled.

course VARCHAR(100)

Example:

ADCA
Java
Python
Web Development
React Native

17. Student Address

The student's address can contain a longer text value.

address TEXT

The TEXT type is useful when the address can contain more characters than a short VARCHAR field.

18. Created Date

The created_at field records when the record was created.

created_at TIMESTAMP
DEFAULT CURRENT_TIMESTAMP

The application does not need to send this value manually when creating a record.

19. Users and Students Relationship

The users table is used for application authentication, while the students table contains student records.

users
  |
  | Authentication
  |
React Native
  |
  | JWT
  ↓
students API
  |
  ↓
students table

The authenticated user does not directly connect to MySQL. All access goes through the API.

20. Database Connection

PHP will connect to MySQL using PDO.

$pdo = new PDO(
    "mysql:host=localhost;dbname=student_management",
    "root",
    ""
);

In a real production environment, database credentials should be protected and should not be exposed in the mobile application.

21. Prepared Statements

Student queries should use prepared statements.

$stmt = $pdo->prepare(
    "SELECT * FROM students
     WHERE id = ?"
);

$stmt->execute([$id]);

Prepared statements help protect the API against SQL injection.

22. Sample Student Records

ID Name Email Mobile Course
1 Rahul Kumar rahul@example.com 9876543210 ADCA
2 Amit Kumar amit@example.com 9876501234 Python
3 Neha Singh neha@example.com 9876512345 Web Development

23. API Database Flow

React Native
      ↓
Axios Request
      ↓
PHP REST API
      ↓
Validate Request
      ↓
PDO
      ↓
MySQL
      ↓
JSON Response
      ↓
React Native

This separation keeps the mobile application independent from the database.

24. Database Operations

The students table will support the four basic CRUD operations.

Operation SQL
Create INSERT
Read SELECT
Update UPDATE
Delete DELETE

25. Search Database Query

The API will later support student searching.

SELECT *
FROM students
WHERE name LIKE ?
   OR email LIKE ?
   OR mobile LIKE ?;

Values should be supplied through prepared statement parameters.

26. Pagination Query

Pagination can be implemented using LIMIT and OFFSET.

SELECT *
FROM students
ORDER BY id DESC
LIMIT ? OFFSET ?;

The API can calculate the offset using:

$offset = ($page - 1) * $limit;

27. Recommended Project Database Structure

student_management
│
├── users
│   ├── id
│   ├── name
│   ├── email
│   ├── password
│   ├── role
│   └── created_at
│
└── students
    ├── id
    ├── name
    ├── email
    ├── mobile
    ├── course
    ├── address
    └── created_at

28. Database Security

  • Never store plain passwords.
  • Use password_hash().
  • Use password_verify() during login.
  • Use PDO prepared statements.
  • Validate API input.
  • Protect database credentials.
  • Do not expose MySQL directly to React Native.
  • Use JWT authentication for protected APIs.
  • Use HTTPS in production.

29. Complete SQL Setup

CREATE DATABASE student_management;

USE student_management;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role VARCHAR(30) NOT NULL DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100),
    address TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

This creates the basic database structure required for our project.

30. Database Design to API Flow

MySQL Database
      ↓
PDO Connection
      ↓
PHP REST API
      ↓
JWT Authentication
      ↓
JSON Response
      ↓
Axios
      ↓
TypeScript
      ↓
React Native Screens

Our database is now ready for the next stage of the project. In the following lessons, we will create the registration API, login API, JWT authentication, and student management APIs step by step.

📌 Key Points

  • The project database is named student_management.
  • The main tables are users and students.
  • The users table stores authentication information.
  • The students table stores student records.
  • Passwords must be stored as secure hashes.
  • PDO prepared statements should be used for database queries.
  • CRUD operations will be performed through the REST API.
  • Search and pagination will be supported by the student API.
  • React Native will never connect directly to MySQL.
  • The next lesson will create the User Registration API.

🧠 Quick Quiz

Question: Which table stores the authentication information of application users?