Lesson 51 of 158 – Create MySQL Tables
51%

Create MySQL Tables

After creating the student_api database, the next step is to create tables for storing application data. In this lesson, we will learn how to create MySQL tables and prepare the database for our PHP REST API.

Note: A database can contain multiple tables. Each table stores a specific type of related data.

1. What is a Table?

A table is a structure inside a database used to store related data.

For example, a student table can store student information such as name, email, mobile number, and course.

student_api
     ↓
students table

2. Why Do We Need Tables?

The database itself is a container. Tables are used to organize the actual application data.

Our REST API may need tables for:

  • Users
  • Students
  • Courses
  • Other application data

3. Select the Database

Before creating a table, select the database where the table should be created.

USE student_api;

This tells MySQL that the following table operations should be performed inside the student_api database.

4. CREATE TABLE Statement

The SQL command used to create a table is:

CREATE TABLE table_name (
    column_name data_type
);

We specify the table name, columns, and data types.

5. Create a Students Table

For our Student Management API, we can create a basic students table.

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100)
);

This creates five columns for storing student information.

6. Table Columns

Columns define the type of information stored in a table.

students

id
name
email
mobile
course

Each student record will contain values for these columns.

7. ID Column

The id column can be used to uniquely identify each student.

id INT

An integer is commonly used for an ID column.

8. VARCHAR Data Type

The VARCHAR data type is commonly used for text values.

name VARCHAR(100)

The number specifies the maximum character length for that column.

9. Email Column

The email address can be stored in a VARCHAR column.

email VARCHAR(150)

This provides enough space for most normal email addresses.

10. Mobile Column

A mobile number can also be stored as text.

mobile VARCHAR(20)

Storing phone numbers as text can preserve values such as country codes and leading zeros.

11. Course Column

The course name can be stored using a VARCHAR column.

course VARCHAR(100)

For example:

ADCA
Java
Python
React Native

12. Primary Key

A primary key uniquely identifies each record in a table.

For our students table, the id column is a suitable primary key.

id INT PRIMARY KEY

13. AUTO_INCREMENT

AUTO_INCREMENT allows MySQL to automatically generate a new numeric ID for each inserted record.

id INT AUTO_INCREMENT PRIMARY KEY

For example, the IDs can be generated as:

1
2
3
4
5

14. NOT NULL

The NOT NULL constraint prevents a column from receiving a NULL value.

name VARCHAR(100) NOT NULL

This can be used when a value is required.

15. Improved Students Table

We can combine the primary key, AUTO_INCREMENT, and NOT NULL constraints.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100)
);

16. Created Table Structure

After creating the table, its basic structure will look like:

students
-------------------------
id
name
email
mobile
course

17. SHOW TABLES

The SHOW TABLES command displays tables inside the currently selected database.

SHOW TABLES;

You should see:

students

18. DESCRIBE Table

The DESCRIBE command displays the structure of a table.

DESCRIBE students;

It can show the column names, data types, NULL settings, keys, and other properties.

19. DESC Shortcut

DESC is a shorter form of DESCRIBE.

DESC students;

It can be used to quickly inspect the structure of the table.

20. IF NOT EXISTS

If a table may already exist, use IF NOT EXISTS.

CREATE TABLE IF NOT EXISTS students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100)
);

This prevents an error when the table already exists.

21. Multiple Tables

A REST API project can have multiple related tables.

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

Each table can store a different category of information.

22. Users Table

A users table can store login information for users of the application.

users
----------------
id
name
email
password

We will work with authentication-related tables later in the course.

23. Courses Table

A courses table can store information about available courses.

courses
----------------
id
course_name
duration
fee

Tables can be designed according to the requirements of the application.

24. Table and REST API

The REST API will use SQL queries to work with the tables.

React Native
      ↓
PHP REST API
      ↓
SQL Query
      ↓
students table

For example, a GET API can retrieve student records from the table.

25. Common Table Creation Errors

  • Forgetting the database selection
  • Missing commas between columns
  • Using an invalid data type
  • Missing closing parenthesis
  • Using a duplicate table name
  • Incorrect primary key definition

Carefully check the SQL syntax when creating a table.

26. Check the Table

After creating the table, use:

SHOW TABLES;

Then inspect the structure using:

DESCRIBE students;

These commands help verify that the table was created correctly.

27. Complete SQL Example

Here is a complete example for creating our students table:

USE student_api;

CREATE TABLE IF NOT EXISTS students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150),
    mobile VARCHAR(20),
    course VARCHAR(100)
);

SHOW TABLES;

DESCRIBE students;

28. Database Structure After Table Creation

student_api
│
└── students
    ├── id
    ├── name
    ├── email
    ├── mobile
    └── course

Now the database has a table that can store student records.

29. Prepare for the REST API

Once the database and tables are ready, PHP can connect to them and perform CRUD operations.

Create → INSERT
Read   → SELECT
Update → UPDATE
Delete → DELETE

These operations will become the foundation of our REST API.

30. Create MySQL Tables Summary

In this lesson, we created the structure required to store student data in MySQL. We learned about tables, columns, data types, primary keys, AUTO_INCREMENT, NOT NULL, and useful commands such as SHOW TABLES and DESCRIBE.

student_api
      ↓
students
      ↓
id
name
email
mobile
course

📌 Key Points

  • A table stores related data inside a database.
  • Use CREATE TABLE to create a table.
  • Use USE student_api; to select the database.
  • The students table stores student information.
  • INT is commonly used for numeric IDs.
  • VARCHAR is used for text values.
  • PRIMARY KEY uniquely identifies records.
  • AUTO_INCREMENT automatically generates numeric IDs.
  • NOT NULL makes a value required.
  • SHOW TABLES; displays database tables.
  • DESCRIBE students; displays table structure.
  • Tables provide the foundation for REST API CRUD operations.

🧠 Quick Quiz

Question: Which SQL statement is used to create a new table?