MySQL Introduction
MySQL is one of the most widely used database systems in the world, and it is free.
What is MySQL?
- MySQL is a relational database management system (RDBMS)
- It stores data in tables made of rows and columns
- You talk to it using SQL
- It is open source and free to use (the Community Edition)
- It was first released in 1995 and is now developed by Oracle
A popular copy of MySQL called MariaDB works almost the same way. Many hosting companies offer MariaDB, and everything in this tutorial works there too.
Server, Client and Database
MySQL works as a server: a program that runs in the background, keeps the data safe on disk and answers requests.
| Part | What it is |
|---|---|
| MySQL server | The program that stores data and runs queries |
| Client | A tool you use to send SQL, such as the mysql command line, MySQL Workbench or phpMyAdmin |
| Database | A named group of tables, for example shop |
| Table | One set of rows and columns, for example products |
One server can hold many databases. A web app, like an online shop, connects to the server, sends SQL and shows the results on its pages.
Where is MySQL Used?
- WordPress and most PHP websites keep their posts, pages and users in MySQL
- E-commerce stores keep products, customers and orders in it
- Apps written in Node.js, Python, Java and PHP connect to it easily
- Large companies such as Facebook, YouTube and Booking.com have used it at huge scale
The Sample Shop Database
This tutorial builds a database called shop for a small store in Karachi. The products table:
Example
SELECT * FROM products;+----+------------------+-------------+---------+-------+ | id | name | category | price | stock | +----+------------------+-------------+---------+-------+ | 1 | Basmati Rice 5kg | Grocery | 2450.00 | 40 | | 2 | Cooking Oil 1L | Grocery | 620.00 | 75 | | 3 | Tea 950g | Grocery | 1650.00 | 30 | | 4 | USB Cable | Electronics | 450.00 | 120 | | 5 | Power Bank | Electronics | 3800.00 | 15 | | 6 | School Bag | Stationery | 2200.00 | 0 | +----+------------------+-------------+---------+-------+ 6 rows in set (0.00 sec)
Prices are in Pakistani Rupees. The orders table stores who bought which product:
Example
SELECT * FROM orders;+----+------------+----------+----------+------------+ | id | product_id | quantity | customer | order_date | +----+------------+----------+----------+------------+ | 1 | 1 | 2 | Ayesha | 2026-10-01 | | 2 | 4 | 3 | Ali | 2026-10-02 | | 3 | 5 | 1 | Bilal | 2026-10-02 | | 4 | 2 | 5 | Ayesha | 2026-10-05 | +----+------------+----------+----------+------------+ 4 rows in set (0.00 sec)
product_id links each order to a row in products. For example, order 1 is 2 bags of Basmati Rice.
The box drawn around the results, and the "rows in set" line, is how the mysql command-line client shows results. Workbench and phpMyAdmin show the same data as a grid.
MySQL Compared with Other Databases
| MySQL | PostgreSQL | MongoDB | |
|---|---|---|---|
| Type | Relational (tables) | Relational (tables) | Document (JSON-like) |
| Query language | SQL | SQL | MongoDB query API |
| Best known for | Websites, WordPress, PHP | Advanced features, strict rules | Flexible data, JavaScript apps |
Exercise
Exercise
Which of these is a MySQL client, a tool you use to send SQL to the server?
