TExcelTAD
Home › MySQL › MySQL Intro

MySQL Intro

MySQL is an open-source relational database management system (RDBMS). It stores data in tables, lets many programs read and change that data at the same time, and keeps it safe on disk when the power goes out. You talk to it with SQL (Structured Query Language), the standard language of relational databases.

MySQL was first released in 1995. Today it is owned by Oracle, and it powers a huge part of the web: WordPress, Drupal and countless online shops run on it, and large sites such as Facebook, Booking.com and GitHub have used it at enormous scale. Its community fork MariaDB, started by MySQL's original author, is almost a drop-in replacement and ships as "MySQL" in many Linux distributions.

mysql · try it yourself
SELECT 'Hello, MySQL!' AS greeting, 6 * 7 AS answer;
+---------------+--------+
| greeting      | answer |
+---------------+--------+
| Hello, MySQL! |     42 |
+---------------+--------+

What is an RDBMS?

A relational database organises data into tables (also called relations). Every table has named columns with a fixed data type, and holds any number of rows (records).

Term Meaning Example in a shop
Table A set of records about one kind of thing customers, products, orders
Row One record the customer Ana Silva
Column One attribute, with a data type email VARCHAR(80)
Primary key A column whose value identifies each row uniquely customer_id
Foreign key A column pointing at a row in another table orders.customer_id

The "relational" part is about these links between tables. Instead of copying a customer's name into every order, an order stores just the customer's id, and SQL joins the tables back together when you ask a question. Each fact lives in exactly one place, so it can't get out of sync.

mysql · try it yourself
SELECT customer_id, first_name, last_name, country
FROM customers
WHERE customer_id <= 3;
+-------------+------------+-----------+----------+
| customer_id | first_name | last_name | country  |
+-------------+------------+-----------+----------+
|           1 | Ana        | Silva     | Portugal |
|           2 | Ben        | Carter    | UK       |
|           3 | Chloe      | Martin    | France   |
+-------------+------------+-----------+----------+

Client and server

MySQL is a client–server system. The server (mysqld) owns the data files and listens for connections. Clients connect to it, send SQL statements and receive results:

  • the command-line client mysql,
  • graphical tools such as MySQL Workbench, DBeaver or phpMyAdmin,
  • your own programs, through a driver (PHP's PDO, Python's mysql-connector, Java's JDBC, Node's mysql2, …).

A typical terminal session looks like this:

bash
$ mysql -u root -p shop
Enter password: ********
mysql> SELECT COUNT(*) FROM customers;
+----------+
| COUNT(*) |
+----------+
|       15 |
+----------+
1 row in set (0.00 sec)

How the examples on this site work

There is no MySQL server inside your browser, so the mysql playgrounds on this site are simulated: pressing Run shows the saved result. Every one of those results was produced by really running the example on a database server (MariaDB 11.8, which speaks the same SQL as MySQL 8 for everything in this course) and is checked automatically. Results are printed the way the mysql client prints them when it reads a script: a table with +---+ borders for each query, nothing at all for statements such as INSERT or CREATE TABLE, and an ERROR … line if a statement fails, which also stops the script.

Each example starts from a fresh copy of the shop sample database (described in the next lesson), so you can insert, update and drop things without breaking later examples.

mysql · try it yourself
SELECT COUNT(*) AS customers FROM customers;
SELECT COUNT(*) AS products FROM products;
SELECT * FROM nonexistent_table;
SELECT 'this line never runs';
+-----------+
| customers |
+-----------+
|        15 |
+-----------+
+----------+
| products |
+----------+
|       20 |
+----------+
ERROR 1146 (42S02) at line 3: Table 'shop.nonexistent_table' doesn't exist

Tip: To experiment for real, install MySQL Community Server (or MariaDB) locally, or run it with Docker: docker run -e MYSQL_ROOT_PASSWORD=secret -p 3306:3306 mysql:8.4. Then load the sample database script and type the examples into the mysql client.

MySQL or MariaDB?

For learning SQL the two are interchangeable. They diverged in some advanced areas (JSON storage, a few functions, error-message wording, replication features). Where that matters, a lesson points it out and shows the MySQL 8 form.

Test yourself

What does RDBMS stand for?

  1. Rapid Data Backup Management System
  2. Relational Database Management System
  3. Remote Database Messaging Service
  4. Row-based Data Modelling Standard

In a relational database, how does an order usually refer to its customer?

  1. It copies the customer's name and address
  2. It stores the customer's primary key value (a foreign key)
  3. It stores the row number of the customer
  4. It cannot; orders and customers must be in the same table

Which program is the MySQL server?

  1. mysql
  2. mysqld
  3. mysqldump
  4. Workbench