Lesson 14 of 60 – MySQL Tables
23%

MySQL Tables

Tables are one of the most important parts of a MySQL database. A table stores data in rows and columns. For example, a student table can store student ID, name, mobile number, and course information.

Note: A database can contain many tables. Each table should normally store data about a specific type of entity or subject.

1. What is a Table?

A table is a structured collection of related data organized into rows and columns.

students
-------------------------
id | name  | course
1  | Rahul | Python
2  | Amit  | Java

2. Tables Inside a Database

A database can contain multiple tables.

school
   |
   ├── students
   ├── teachers
   ├── courses
   ├── attendance
   └── fees

Each table can store information related to a particular part of the application.

3. Rows in a Table

A row represents one complete record in a table.

id | name  | course
1  | Rahul | Python

The above row represents one student record.

4. Columns in a Table

A column represents a particular type of information stored for each record.

id
name
course

In the students table, id, name, and course are columns.

5. Example Students Table

id name course
1 Rahul Python
2 Amit Java
3 Neha SQL

6. Creating a Table

The CREATE TABLE statement is used to create a table.

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

7. Table Name

The name immediately after CREATE TABLE is the table name.

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

Here, students is the table name.

8. Table Columns

Each column definition normally contains a column name and a data type.

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

9. Data Types in Tables

Every column normally has a data type that describes the kind of data it can store.

id      INT
name    VARCHAR(100)
salary  DECIMAL(10,2)
dob     DATE

10. Primary Key in a Table

A primary key uniquely identifies rows in a table.

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

Here, id is the primary key.

11. Inserting Rows

Use INSERT INTO to add records to a table.

INSERT INTO students
(id, name, course)
VALUES
(1, 'Rahul', 'Python');

12. Viewing Table Data

The SELECT statement is used to retrieve data.

SELECT * FROM students;

The asterisk means all columns.

13. Viewing Specific Columns

You can select specific columns from a table.

SELECT name, course
FROM students;

Only the name and course columns are returned.

14. Viewing Table Structure

The DESCRIBE statement displays information about a table's columns.

DESCRIBE students;

You can also use:

DESC students;

15. SHOW TABLES

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

USE school;

SHOW TABLES;

16. Table Naming

Use clear and meaningful table names.

Examples:

students
teachers
courses
employees
products
orders

Good names make database structures easier to understand.

17. Table with Multiple Columns

A table can contain many columns according to the requirements of the application.

CREATE TABLE employees (
    id INT,
    name VARCHAR(100),
    email VARCHAR(150),
    mobile VARCHAR(15),
    salary DECIMAL(10,2)
);

18. Adding Multiple Rows

Multiple rows can be inserted in one INSERT statement.

INSERT INTO students
(id, name, course)
VALUES
(1, 'Rahul', 'Python'),
(2, 'Amit', 'Java'),
(3, 'Neha', 'SQL');

19. Updating Table Data

The UPDATE statement changes existing records.

UPDATE students
SET course = 'MySQL'
WHERE id = 1;

The WHERE condition identifies the row to update.

20. Deleting Rows

The DELETE statement removes rows from a table.

DELETE FROM students
WHERE id = 3;

This removes the student whose ID is 3.

21. Adding a Column

The ALTER TABLE statement can be used to add a column.

ALTER TABLE students
ADD mobile VARCHAR(15);

This adds a mobile column to the students table.

22. Renaming a Column

Modern MySQL versions support changing a column name using ALTER TABLE.

ALTER TABLE students
RENAME COLUMN name TO student_name;

The exact ALTER TABLE syntax depends on the operation and MySQL version.

23. Dropping a Column

You can remove a column using ALTER TABLE.

ALTER TABLE students
DROP COLUMN mobile;

The mobile column is removed from the table structure.

24. Dropping a Table

The DROP TABLE statement removes a table and its stored data.

DROP TABLE students;

This is different from DELETE, which removes rows while keeping the table structure.

25. TRUNCATE TABLE

TRUNCATE TABLE removes all rows from a table while keeping the table structure.

TRUNCATE TABLE students;

The table remains available for future use.

26. Table Relationships

Tables can be related using keys such as foreign keys.

students
---------
id
course_id
   |
   ↓
courses
-------
id
course_name

Relationships help organize related data across multiple tables.

27. Good Table Design

Good table design usually includes:

  • Meaningful table names
  • Clear column names
  • Appropriate data types
  • Primary keys where appropriate
  • Proper relationships between tables
  • Suitable constraints

28. Common Table Errors

Common problems while working with tables include:

  • Table does not exist
  • Table already exists
  • Wrong column name
  • Incorrect data type
  • Duplicate primary key
  • No database selected
  • Insufficient privileges

29. Complete Table Example

CREATE DATABASE school;

USE school;

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

INSERT INTO students
(id, name, mobile, course)
VALUES
(1, 'Rahul', '9876543210', 'Python'),
(2, 'Amit', '9876501234', 'Java');

SELECT * FROM students;

DESCRIBE students;

SHOW TABLES;

30. Practical Student Management Table

Suppose you are building a student management application. You can create a students table like this:

CREATE DATABASE IF NOT EXISTS student_management;

USE student_management;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15),
    course VARCHAR(100),
    fee DECIMAL(10,2)
);

INSERT INTO students
(id, name, mobile, course, fee)
VALUES
(1, 'Rahul Kumar', '9876543210', 'Python', 15000.00),
(2, 'Amit Kumar', '9876501234', 'Java', 18000.00);

SELECT * FROM students;

This table can store basic student information for a real-world application.

📌 Key Points

  • A MySQL table stores related data in rows and columns.
  • A row represents a record.
  • A column represents a specific type of information.
  • CREATE TABLE creates a new table.
  • SHOW TABLES lists tables in the selected database.
  • DESCRIBE displays table structure.
  • INSERT adds records to a table.
  • UPDATE changes existing records.
  • DELETE removes selected rows.
  • DROP TABLE removes the complete table.

🧠 Quick Quiz

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