MySQL: Introduction to MySQL and Basic Database Concepts

MySQL is the world's most popular open-source relational database management system—it makes data storage and querying simple and efficient.

This tutorial is designed for beginners with no prior experience. It starts with the basics of databases and progresses until you are proficient in using MySQL for data management.

1. What You'll Learn



2. A Developer’s True Story

(1) Pain Point: Excel Can’t Handle It Anymore

An e-commerce operations manager was assigned an urgent task:

"Compile sales trends for the past 12 months, covering 5,000 customers and 30,000 orders—and present the report to the CEO tomorrow morning."

Open an Excel file: 100,000+ rows of data, file size 50 MB.

The problems came one after another:

(2) Database Solutions

Redesigning with MySQL:

SQL
-- Create a Customer Table
CREATE TABLE customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    email VARCHAR(150)
);

-- Create the Orders Table
CREATE TABLE orders (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT,
    amount DECIMAL(10,2),
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

-- One SQL Identify Annual Sales Trends
SELECT 
    DATE_FORMAT(order_date, '%Y-%m') AS month,
    COUNT(*) AS total_orders,
    SUM(amount) AS total_revenue
FROM orders
WHERE order_date >= '2025-07-01'
GROUP BY month
ORDER BY month;

Yield Comparison:

Dimension Excel MySQL
100,000-row query 30+ seconds < 1 second
Multi-table Joins Manual VLOOKUP Automatic JOIN
Collaborative Editing File Conflicts Concurrent Read/Write Operations Without Conflicts
Data Update Manual Refresh Real-time Query for Latest Data
Data Security Risk of Data Loss Transaction and Backup Protection


3. What Is a Database?

A database is a repository for organizing, storing, and managing data according to a data structure.

(1) Databases vs. File Storage

100%
graph TB
    A[Data Storage Methods] --> B[File Storage]
    A --> C[Database]
    B --> D[Excel / CSV / TXT]
    B --> E[Manual Management,Low efficiency]
    C --> F[Structured Storage]
    C --> G[Efficient Queries,Safe and Reliable]
Dimension File Storage Database
Data Volume Thousands of rows Millions to hundreds of millions of rows
Query Speed Slow (full-file scan) Fast (index optimization)
Concurrent Access Not supported Supports simultaneous read/write access by multiple users
Data Integrity Not Guaranteed Constraints + Transactions Guaranteed
Security File-level User Permissions + Encryption

(2) Database Categories

100%
graph TB
    A[Database] --> B[Relational Database RDBMS]
    A --> C[Non-relational database NoSQL]
    B --> D[MySQL / PostgreSQL]
    B --> E[Oracle / SQL Server]
    B --> F[SQLite]
    C --> G[Key-value type: Redis]
    C --> H[Document-based: MongoDB]
    C --> I[Column Type: Cassandra]
    C --> J[Graph Database: Neo4j]
Type Representative Products Data Model Applicable Scenarios
Relational MySQL, PostgreSQL Tables (rows + columns) Structured data, transaction processing
Key-Value Redis Key-Value Pairs Caching, Sessions, Leaderboards
Document-based MongoDB JSON/BSON documents Semi-structured data, content management
Column Family Type Cassandra Column Family Big Data Analytics, Time Series Data
Graph Database Neo4j Nodes + Edges Social Networks, Recommendation Systems
💡 Tip: MySQL is a relational database; data is stored in the form of tables, and relationships can be established between tables.



4. Core Concepts of Relational Databases

(1) Table

A table is the basic unit for storing data in a database and consists of rows and columns.

100%
graph LR
    subgraph customers Table
        direction TB
        H[id | name | email | created_at]
        R1[1 | Alice | alice@email.com | 2025-01-15]
        R2[2 | Bob | bob@email.com | 2025-02-20]
        R3[3 | Charlie | charlie@email.com | 2025-03-10]
    end
Term Definition Example
Table A collection of data customers Table
Row A specific record Alice's complete information
Column A property of the data name Column
Primary Key A field that uniquely identifies each row id column
Foreign Key A field that references another table orders.customer_id

(2) Relationship Types

100%
erDiagram
    CUSTOMERS ||--o{ ORDERS : "A customer has multiple orders"
    ORDERS ||--|{ ORDER_ITEMS : "An order contains multiple line items"
    PRODUCTS ||--o{ ORDER_ITEMS : "An item appears in multiple orders"
Relationship Type Description Example
One-to-One (1:1) One record corresponds to another record User ↔ ID Card
One-to-many (1:N) One record corresponds to multiple records Customer → Order
Many-to-Many (M:N) Multiple records correspond to each other Students ↔ Courses (via course registration form)

(3) Classification of SQL Languages

SQL (Structured Query Language) is the standard language for working with relational databases.

Category Full Name Keywords Purpose
DDL Data Definition Language CREATE/ALTER/DROP Defines database structure
DML Data Manipulation Language INSERT/UPDATE/DELETE Insert, update, delete data
DQL Data Query Language SELECT Query data
DCL Data Control Language GRANT/REVOKE Permission management
TCL Transaction Control Language COMMIT/ROLLBACK Transaction control


5. The History of MySQL

(1) Version History

100%
timeline
    title MySQL Version History
    1995 : MySQL 1.0 Published
         : From Sweden MySQL AB Company Development
    2000 : MySQL 3.23
         : Supports transaction processing
    2003 : MySQL 4.0
         : Support UNION Search
    2005 : MySQL 5.0
         : Supports stored procedures、View、Trigger
    2008 : Sun Acquisition MySQL AB
    2010 : Oracle Acquisition Sun
         : Obtain MySQL Ownership
    2013 : MySQL 5.6
         : InnoDB Full-Text Search
    2016 : MySQL 5.7
         : JSON Support、Performance Improvements
    2018 : MySQL 8.0
         : Window Functions、CTE、Characters
    2024 : MySQL 8.4 LTS
         : Long-Term Support Version
    2024 : MySQL 9.0 Innovation
         : Latest Innovative Version

(2) MySQL's Design Philosophy

MySQL's Core Design Principles:

Simplicity First

Performance First

Open Ecosystem



6. Key Advantages of MySQL

(1) Comparison of Advantages

Dimension MySQL PostgreSQL Oracle SQL Server
Price Free (open source) Free (open source) Expensive (commercial) Expensive (commercial)
Learning Difficulty ⭐ Easy ⭐⭐ Moderate ⭐⭐⭐ Hard ⭐⭐ Moderate
Read Performance ⭐⭐⭐ Extremely fast ⭐⭐ Fast ⭐⭐ Fast ⭐⭐ Fast
Community Activity ⭐⭐⭐ Very High ⭐⭐ High ⭐ Moderate ⭐ Moderate
Use Cases Web applications, e-commerce Complex queries, GIS Large-scale enterprise systems Windows ecosystem

(2) Typical Use Cases

100%
mindmap
  root((MySQL Applications))
    Web Development
      Blog System
      E-commerce Platform
      Social Networks
      Content Management
    Enterprise Applications
      ERP System
      CRM System
      Inventory Management
      Order Processing
    Data Analysis
      Log Storage
      User Behavior
      Report Statistics
    Mobile Apps
      App Backend
      Game Data
      Push Notification System

Who Uses MySQL?

Company Purpose
Meta (Facebook) User data, social graph
Twitter Tweets, user profiles
GitHub Code repositories, user data
Airbnb Listings, Bookings, Users
Shopify E-commerce backend, products and orders
Booking.com Hotel Reservations, User Reviews


7. A Quick Introduction to MySQL

(1) Connecting to MySQL in Three Steps

Step Command/Action
1. Installation Start Docker with a single command (see Lesson 2 for details)
2. Connection mysql -u root -p
3. Verification SELECT VERSION();

📖 The next lesson will cover in detail: cross-platform installation (Windows/Mac/Linux/Docker), connecting to MySQL Workbench, and security configuration. This section provides only a quick overview.

▶ Example: Starting Docker with a single command

BASH
# Pull and Start MySQL 8.0 (see Lesson 2 for details)
docker run -d --name mysql-tutorial \
  -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=MyPass123! \
  mysql:8.0

# Enter MySQL Command Line
docker exec -it mysql-tutorial mysql -u root -pMyPass123!

Output:

TEXT
+---------+
| VERSION |
+---------+
| 8.0.35  |
+---------+
1 row in set

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| testdb             |
+--------------------+

▶ Example: Quick Verification After Connection

SQL
-- View MySQL Version
SELECT VERSION();

-- View All Databases
SHOW DATABASES;
▶ Try it Yourself

Output:

TEXT
+-----------+
| VERSION() |
+-----------+
| 8.0.36    |
+-----------+

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

▶ Example: Creating Your First Database and Table

Output:

TEXT
Query OK, 1 row affected

Database changed

Query OK, 0 rows affected

Query OK, 1 row affected

Query OK, 1 row affected

---+---------+------------------
id | name    | email            
---+---------+------------------
1  | Alice   | alice@email.com  
2  | Bob     | bob@email.com    
3  | Charlie | charlie@email.com
---+---------+------------------
3 rows in set
SQL
-- 1. Create a Database
CREATE DATABASE my_first_db;

-- 2. Using the Database
USE my_first_db;

-- 3. Create a user table
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 4. Insert Data
INSERT INTO users (username, email) VALUES ('Alice', 'alice@example.com');
INSERT INTO users (username, email) VALUES ('Bob', 'bob@example.com');

-- 5. Query Data
SELECT * FROM users;

Output:

TEXT
Output displayed

Output:

TEXT
+----+----------+-----------------+---------------------+
| id | username | email           | created_at          |
+----+----------+-----------------+---------------------+
|  1 | Alice    | alice@example.com | 2026-07-03 10:30:00 |
|  2 | Bob      | bob@example.com   | 2026-07-03 10:30:01 |
+----+----------+-----------------+---------------------+

▶ Example: CRUD Operations on Data

SQL
-- Conditional Query
SELECT username, email FROM users WHERE username = 'Alice';

-- Update Data
UPDATE users SET email = 'alice_new@example.com' WHERE id = 1;

-- Delete Data
DELETE FROM users WHERE id = 2;

-- Count the number of users
SELECT COUNT(*) AS total_users FROM users;
▶ Try it Yourself

Output:

TEXT
Output displayed

❓ FAQ

Q What is the relationship between MySQL and SQL?
A SQL is the standard language for operating databases (similar to "English"), while MySQL is database software that implements the SQL standard (similar to "the English spoken in the UK").
Q Is MySQL free? Can it be used for commercial purposes?
A MySQL Community Edition is free and open source (under the GPL license) and can be used for commercial purposes. However, if you modify the MySQL source code and distribute it, you must also release it as open source. Oracle offers a commercial version (for a fee) that provides additional enterprise features.
Q Which should I choose, MySQL or MariaDB?
A MariaDB is a fork of MySQL and is highly syntax-compatible with it. For new projects, MySQL 8.0+ is the mainstream choice; if you have concerns about Oracle’s open-source strategy, MariaDB is a good alternative.
Q Do I need to learn a programming language before learning MySQL?
A No. SQL is a standalone language with syntax similar to natural English. You can learn it directly in the MySQL command line. Later lessons will cover how to connect to MySQL using Node.js or Python.
Q What size of projects is MySQL suitable for?
A MySQL is suitable for everything from small personal projects to large-scale enterprise applications. A single table can store hundreds of millions of rows of data, and with database and table sharding, it can support even larger scales. Meta, Twitter, and GitHub all use MySQL.
Q Can MySQL be used on Windows, Mac, and Linux?
A Yes. MySQL officially supports all platforms. We recommend installing it using Docker—set it up once, run it anywhere.

📖 Summary


📝 Exercises

  1. Basic Exercise (Difficulty: ⭐): Install MySQL 8.0 using Docker. Once connected, run SELECT VERSION(); to check the version number.

  2. Advanced Problem (Difficulty ⭐⭐): Create a database named school that contains a table named students (id, name, age, grade), insert 5 records, and query all records.

  3. Challenge Question (Difficulty: ⭐⭐⭐): Create the courses table and the student_courses associated table (many-to-many relationship), design appropriate fields, and use a JOIN to query all courses taken by a specific student.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏