MySQL Basics: Databases, Tables and Queries

Database ~ 43 minutes

# Welcome to MySQL

Hello and welcome! Today, we will learn the basics of MySQL. MySQL is one of the most popular databases in the world. Websites, shops and mobile apps use it to keep their data safe and in order.

A database is like a big cabinet. Inside the cabinet, we have tables. A table is like a sheet in a spreadsheet. It has columns, which are the headings, and rows, which are the records. To talk to MySQL, we use a language called SQL. SQL means Structured Query Language.

What you will learn

  • How to connect to MySQL
  • How to create a database and a table
  • How to add, read, change and remove data
  • How to join two tables
  • How to make a backup

Do not worry if these words are new to you. We will go slowly, one small step at a time. You can also keep the MySQL reference manual open in another tab.

Check that MySQL is installed
Code Copied to Clipboard

# Reading the result

If you see a version number, such as mysql Ver 8.0, then MySQL is installed. That is great news! If you see command not found, please install MySQL first. You can download it from the MySQL download page. Many people on Windows also use tools like XAMPP or Laragon, which include MySQL.

Also, make sure the MySQL server is running. On many systems, it starts by itself. If it is not running, start it from your control panel or service manager.

Now let us connect to the server. We will use the root user in this tutorial, because it is only for learning. In a real project, you should create a user with fewer rights.

Connect to MySQL
Code Copied to Clipboard

# Enter your password

MySQL will ask for your password. Please type it and press Enter. The letters will not show on the screen. This is normal, so do not worry.

After you connect, your prompt changes to mysql>. From now on, everything you type is an SQL statement. Each statement must end with a semicolon ;. The semicolon tells MySQL that you have finished the line.

Let us create our first database. We will call it shop. Then we will tell MySQL to use it. The word USE means please work inside this database from now on.

Create a database
Code Copied to Clipboard

# Create your first table

Now, we have an empty database. A database with no tables is like an empty cabinet. Let us add a table called products. For each column, we must choose a name and a data type.

Common data types

  • INT is for whole numbers
  • VARCHAR(100) is for short text, up to 100 letters
  • TEXT is for long text
  • DECIMAL(10,2) is for money, with two numbers after the dot
  • DATETIME is for a date and a time

We also add a primary key. This is a column that gives each row its own unique number. We use AUTO_INCREMENT so MySQL counts up for us.

Create the products table
Code Copied to Clipboard

Why use NOT NULL?

The words NOT NULL mean that the column can never be empty. This protects your data. For example, a product without a name makes no sense, so we block it.

The word DEFAULT gives a starting value when you do not give one. Here, stock starts at zero, and created_at takes the current time. Small rules like these keep your tables clean and save you from many bugs later.

# Add data with INSERT

Our table is ready, but it is empty. Let us add some rows. We use the INSERT INTO statement. First, we list the columns. Then, we give the values in the same order.

Notice that we do not give the id. MySQL makes it for us. We also skip created_at, because it has a default value.

Text values go inside single quotes, like 'Pen'. Numbers do not need quotes. You can add many rows in one statement. Just separate each group of values with a comma.

Insert some products
Code Copied to Clipboard

# Read data with SELECT

Now comes the most used statement in SQL. It is SELECT. It reads data from a table. The star * means all columns. You can also name only the columns you want.

The WHERE part filters the rows. The ORDER BY part sorts them. The LIMIT part keeps only the first few rows. You can mix these parts together to ask very clear questions.

Let us try a few examples. After each one, look at the result and see how it changes.

Select examples
Code Copied to Clipboard

# Change and remove data

Sometimes data changes. The price goes up, or a product is sold out. We use UPDATE to change rows, and DELETE to remove rows.

Both statements need a WHERE part. Please read the next section carefully, because this is the most common mistake that beginners make.

Never forget WHERE

If you write UPDATE or DELETE without a WHERE part, MySQL changes or removes every row in the table. There is no undo button!

Safe habits

  • First run a SELECT with the same WHERE part, and check the rows
  • Then change SELECT * into UPDATE or DELETE
  • Make a backup before big changes
  • Test on a copy of your data, never first on the real one

These small habits can save your whole project one day.

Update and delete
Code Copied to Clipboard

# Joining two tables

Real databases have many tables. Let us say we also have an orders table. Each order stores a product_id. This number points to a row in products. We call it a foreign key.

To see the product name together with each order, we use a JOIN. A join joins rows from two tables when a column matches. The most common type is INNER JOIN. It shows only the rows that have a match on both sides.

This is how big systems keep data tidy. The product name is saved only once, and the orders just point to it. If the name changes, you fix it in one place only.

A simple join
Code Copied to Clipboard

Add an index for speed

When a table grows to thousands of rows, searches can become slow. An index works like the index of a book. MySQL can jump to the right place and does not have to read every row.

The primary key already has an index. For other columns that you search often, such as email, you can add one with CREATE INDEX. Do not add too many, because each index uses some space and makes writing a little slower.

# Leave MySQL and make a backup

To leave the MySQL prompt, type exit and press Enter. You will return to your normal terminal.

Now let us learn one more very useful skill, which is making a backup. MySQL comes with a tool called mysqldump. It writes your whole database into a text file full of SQL statements. You can load this file later to restore your data.

The sign > sends the result into a file. Please run the command below from your normal terminal, not from the mysql> prompt. It will ask for your password again.

Back up the shop database
Code Copied to Clipboard

# Summary and next steps

Wonderful! You have learned the core of MySQL. Let us review.

Summary

  • A database holds tables, and a table holds rows and columns
  • CREATE DATABASE and CREATE TABLE build the structure
  • INSERT, SELECT, UPDATE and DELETE handle the data
  • WHERE filters rows, and you must never forget it
  • JOIN connects two tables, and mysqldump makes backups

What to learn next

  • GROUP BY and totals such as SUM and AVG
  • Foreign key rules and transactions
  • Using MySQL from PHP with PDO or Laravel

The MySQL tutorial is a good next stop. Keep practicing, and you will become confident very soon.