LearnContact
Lesson 5720 min read

Introduction to MySQL

Learn what MySQL is, how relational databases organize data in tables, and the basic SQL commands you will use to work with them from PHP.

Introduction

Almost every dynamic website you have ever used — a store, a forum, a social network, a blogging platform — needs somewhere to permanently store its data. Users, products, orders, comments, and posts all have to live somewhere between one visit and the next. That "somewhere" is almost always a database, and for PHP developers, the most common choice by far is MySQL.

Before you connect PHP to a database in the next lesson, this lesson introduces MySQL on its own terms: what it is, how it organizes data, and the basic SQL vocabulary you need to read and write queries.

What You Will Learn
  • What MySQL is and what "relational database" means.
  • Why PHP applications are so commonly paired with MySQL.
  • How data is organized into tables, rows, and columns.
  • The core SQL commands: SELECT, INSERT, UPDATE, DELETE, and CREATE TABLE.
  • What phpMyAdmin is and how it helps you manage a database visually.

What is MySQL?

MySQL is a relational database management system (RDBMS) — software that stores data in structured tables and lets you query, insert, update, and delete that data using a language called SQL (Structured Query Language). It is free, open source, extremely fast for typical web workloads, and has been the default database choice for PHP developers for over two decades.

A "relational" database organizes data into separate tables that can be linked ("related") to one another. For example, a `students` table might relate to a `grades` table through a shared student ID, rather than cramming everything into one giant table.

Relational

Data lives in structured tables that can be related to one another through shared keys.

Free & Open Source

MySQL is free to download, install, and use, with a huge community and long track record.

Fast & Reliable

Optimized for the read/write patterns typical of web applications.

Speaks SQL

You interact with MySQL using SQL, a standardized query language shared by many database systems.

Why PHP and MySQL Go Together

PHP was designed from the beginning with strong, built-in support for talking to databases, and MySQL was one of the very first it supported well. Together they became the backbone of the "LAMP stack" (Linux, Apache, MySQL, PHP) that powered a huge share of the early web — and still powers platforms like WordPress today.

The pairing works so well because both are free, both are easy to install locally (for example, through XAMPP), and PHP has multiple built-in extensions — MySQLi and PDO, which you will meet in upcoming lessons — dedicated specifically to communicating with MySQL.

Why This Matters

Without a database, a PHP application can only work with data that exists during a single request. MySQL gives your application permanent, structured storage that survives between visits, page loads, and server restarts.

Tables, Rows & Columns

A MySQL database is made up of one or more tables. Each table has a fixed set of columns (also called fields) that define what kind of data it stores, and any number of rows (also called records), where each row is a single entry.

idnameagecourse
1Amit Verma21PHP Development
2Sara Khan23Web Design
3Rahul Singh20Database Systems

In the table above, `students` is the table name, `id`, `name`, `age`, and `course` are the columns, and each numbered row is one student record. The `id` column is typically the primary key — a unique value that identifies each row unambiguously.

Database

A container that holds one or more related tables.

Table

A structured collection of rows sharing the same columns.

Row (Record)

A single entry in a table — one student, one order, one comment.

Column (Field)

A single piece of data every row has, such as `name` or `age`.

Primary Key

A column (often `id`) that uniquely identifies each row in a table.

Creating a Table

Tables are created with the CREATE TABLE statement, where you list each column name alongside its data type.

create-table.sql
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INT NOT NULL,
    course VARCHAR(100)
);
Reading This Statement
  • `INT` stores whole numbers; `VARCHAR(100)` stores text up to 100 characters.
  • `AUTO_INCREMENT` makes MySQL automatically assign the next number to `id` for each new row.
  • `PRIMARY KEY` marks `id` as the unique identifier for the table.
  • `NOT NULL` means that column cannot be left empty.

Basic SQL Commands

Once a table exists, you use four core SQL commands to work with its data. Together they are often remembered by the acronym CRUD — Create, Read, Update, Delete — which you will explore in depth in an upcoming lesson.

INSERT adds a new row to a table.

insert.sql
INSERT INTO students (name, age, course)
VALUES ('Amit Verma', 21, 'PHP Development');

SELECT reads rows from a table. You can select every column with *, specific columns by name, and filter rows with a WHERE clause.

select.sql
SELECT * FROM students;

SELECT name, course FROM students
WHERE age > 20;

UPDATE changes existing rows. Always include a WHERE clause — an UPDATE without one changes every row in the table.

update.sql
UPDATE students
SET course = 'Advanced PHP'
WHERE id = 1;

DELETE removes rows. Just like UPDATE, forgetting the WHERE clause deletes every row in the table.

delete.sql
DELETE FROM students
WHERE id = 3;
Always Use WHERE

UPDATE and DELETE statements without a WHERE clause apply to every single row in the table. Double-check your WHERE condition before running either command, especially on real data.

Managing MySQL with phpMyAdmin

Typing every SQL command by hand is not the only option. phpMyAdmin is a free, web-based graphical tool for managing MySQL databases, and it comes bundled with XAMPP (the local development environment you likely installed in an earlier lesson).

With phpMyAdmin, you can create databases and tables, browse and edit rows through a spreadsheet-like interface, run raw SQL queries, import and export data, and manage user permissions — all from your browser, without writing a single line of PHP.

Start Apache and MySQL from the XAMPP control panel
Open http://localhost/phpmyadmin in your browser
Create a database and tables using the interface, or the SQL tab
Browse, insert, edit, and delete rows visually
Good for Learning

phpMyAdmin is a great place to practice SQL commands and inspect your data while you are learning, even after you start writing real queries from PHP code.

Common Mistakes

Avoid These Mistakes
  • Confusing MySQL (the database system) with SQL (the language used to talk to it).
  • Forgetting the WHERE clause on UPDATE or DELETE and affecting every row.
  • Not giving every table a primary key, making individual rows hard to reference.
  • Mixing up VARCHAR (text) and INT (numbers) when designing table columns.

Best Practices

  • Give every table a primary key, usually an auto-incrementing `id` column.
  • Choose specific, appropriately sized data types (VARCHAR(100) rather than a huge generic type) for each column.
  • Practice basic SELECT, INSERT, UPDATE, and DELETE statements directly in phpMyAdmin before writing PHP code against them.
  • Name tables and columns clearly and consistently (for example, always lowercase with underscores).

Frequently Asked Questions

Is MySQL the only database PHP can use?

No. PHP can work with many databases, including PostgreSQL, SQLite, and SQL Server, but MySQL is by far the most common pairing historically and in most tutorials.

Do I need to install MySQL separately?

Not if you installed XAMPP — it bundles MySQL (technically MariaDB, a MySQL-compatible fork) along with phpMyAdmin and Apache.

What is the difference between MySQL and SQL?

SQL is the query language itself. MySQL is one specific database system that understands and executes SQL commands — other systems like PostgreSQL also use SQL with minor differences.

Can I manage MySQL without phpMyAdmin?

Yes, through the command-line `mysql` client or other GUI tools, but phpMyAdmin is the most beginner-friendly option and comes ready to use with XAMPP.

What does CRUD stand for?

Create, Read, Update, Delete — the four fundamental operations you perform on stored data, covered in depth in an upcoming lesson.

Key Takeaways

  • MySQL is a free, relational database system that stores data in tables made of rows and columns.
  • PHP and MySQL are commonly paired together, historically forming the "M" in the LAMP stack.
  • The four core SQL commands are SELECT, INSERT, UPDATE, and DELETE.
  • UPDATE and DELETE without a WHERE clause affect every row in a table.
  • phpMyAdmin, bundled with XAMPP, provides a graphical way to manage MySQL databases.

Summary

MySQL gives your PHP applications a place to permanently store structured data, organized into tables of rows and columns and manipulated through SQL commands.

In this lesson, you learned what MySQL is, how relational data is organized, the essential SQL commands, and how phpMyAdmin lets you manage a database visually. Next, you will connect PHP directly to MySQL using the MySQLi extension.

Lesson 57 Completed
  • You understand what a relational database is.
  • You can read and write basic SQL: SELECT, INSERT, UPDATE, DELETE, CREATE TABLE.
  • You know what phpMyAdmin is used for.
  • You are ready to connect PHP to MySQL with MySQLi.
Next Lesson →

Connecting PHP with MySQL (MySQLi)