系统学习 SQL 学习路径:从没写过查询到能独立做报表

建练习库与五张示例表:后面所有页面的数据都在这里

适用场景

已经按 install-mysql 连上 MySQL,准备开始写查询。这一页给出整套练习库 sql_tutorial:五张表、种子数据和重置方法。后面 30 多页的示例都默认你已经执行过本页脚本,不需要每页重建数据。

示例库结构

表 作用 关键关系
users 用户 主键 id
products 商品 主键 id
orders 订单 user_id 指向 users.id
order_items 订单明细 order_id 指向 orders.id,product_id 指向 products.id
accounts 账户余额 只用于事务页的转账示例

orders.total_amount 是订单总额,order_items 里存每一行的数量和成交单价。两者能互相校验,后面的聚合、JOIN 和事务都靠这个关系。

建库与建表

先建库并切换进去:

CREATE DATABASE IF NOT EXISTS sql_tutorial
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_0900_ai_ci;
USE sql_tutorial;

utf8mb4_0900_ai_ci 是 MySQL 8.0 的默认排序规则;如果还在用 MySQL 5.7,改成 utf8mb4_unicode_ci。

用户表与商品表:

CREATE TABLE users (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  city VARCHAR(50),
  email VARCHAR(100) UNIQUE,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE products (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  category VARCHAR(50) NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  stock INT NOT NULL DEFAULT 0
);

订单、明细与账户表:

CREATE TABLE orders (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'pending',
  total_amount DECIMAL(10,2) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE order_items (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT NOT NULL,
  product_id BIGINT NOT NULL,
  quantity INT NOT NULL,
  unit_price DECIMAL(10,2) NOT NULL,
  CONSTRAINT fk_items_order FOREIGN KEY (order_id) REFERENCES orders(id),
  CONSTRAINT fk_items_product FOREIGN KEY (product_id) REFERENCES products(id)
);

CREATE TABLE accounts (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  owner VARCHAR(50) NOT NULL,
  balance DECIMAL(12,2) NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
    ON UPDATE CURRENT_TIMESTAMP
);

插入种子数据

用户 8 行,故意留了空的 city、空的 email,以及一个没有订单的用户:

INSERT INTO users (id, name, city, email, created_at) VALUES
  (1, 'Alice',  'Beijing',  'alice@example.com',  '2026-01-05 09:00:00'),
  (2, 'Bob',    'Shanghai', 'bob@example.com',    '2026-01-08 10:30:00'),
  (3, 'Carol',  'Beijing',  NULL,                 '2026-01-12 14:15:00'),
  (4, 'David',  NULL,       'david@example.com',  '2026-02-01 08:45:00'),
  (5, 'Eve',    'Shenzhen', 'eve@example.com',    '2026-02-10 16:20:00'),
  (6, 'Frank',  'Shanghai', 'frank@example.com',  '2026-02-15 11:10:00'),
  (7, 'Grace',  'Hangzhou', 'grace@example.com',  '2026-03-01 19:05:00'),
  (8, 'Heidi',  'Beijing',  'heidi@example.com',  '2026-03-08 07:50:00');

商品 8 行,覆盖五个分类,其中 USB-C 扩展坞 库存为 0:

INSERT INTO products (id, name, category, price, stock) VALUES
  (1, '机械键盘',     '外设',     399.00, 30),
  (2, '无线鼠标',     '外设',     129.00, 50),
  (3, '显示器',       '显示设备', 1299.00, 12),
  (4, 'USB-C 扩展坞', '外设',     299.00, 0),
  (5, '降噪耳机',     '音频',     899.00, 20),
  (6, '蓝牙音箱',     '音频',     349.00, 15),
  (7, '笔记本支架',   '桌面',     159.00, 40),
  (8, '移动硬盘',     '存储',     599.00, 8);

订单 10 行,覆盖 pending/paid/shipped/cancelled 四种状态:

INSERT INTO orders (id, user_id, status, total_amount, created_at) VALUES
  (1,  1, 'paid',      528.00,  '2026-03-01 10:00:00'),
  (2,  2, 'shipped',   1587.00, '2026-03-02 11:30:00'),
  (3,  1, 'cancelled', 299.00,  '2026-03-03 09:15:00'),
  (4,  3, 'pending',   899.00,  '2026-03-05 15:40:00'),
  (5,  4, 'paid',      508.00,  '2026-03-06 13:20:00'),
  (6,  5, 'shipped',   599.00,  '2026-03-08 08:10:00'),
  (7,  2, 'paid',      258.00,  '2026-03-10 17:45:00'),
  (8,  6, 'cancelled', 399.00,  '2026-03-12 12:00:00'),
  (9,  7, 'pending',   159.00,  '2026-03-15 10:25:00'),
  (10, 1, 'paid',      478.00,  '2026-03-18 16:00:00');

订单明细 15 行,金额之和与订单总额一致:

INSERT INTO order_items (id, order_id, product_id, quantity, unit_price) VALUES
  (1,  1,  1, 1, 399.00),
  (2,  1,  2, 1, 129.00),
  (3,  2,  3, 1, 1299.00),
  (4,  2,  7, 1, 159.00),
  (5,  2,  2, 1, 129.00),
  (6,  3,  4, 1, 299.00),
  (7,  4,  5, 1, 899.00),
  (8,  5,  6, 1, 349.00),
  (9,  5,  7, 1, 159.00),
  (10, 6,  8, 1, 599.00),
  (11, 7,  2, 2, 129.00),
  (12, 8,  1, 1, 399.00),
  (13, 9,  7, 1, 159.00),
  (14, 10, 6, 1, 349.00),
  (15, 10, 2, 1, 129.00);

账户 2 行,只用于 transactions 的转账示例:

INSERT INTO accounts (id, owner, balance) VALUES
  (1, 'Alice', 1000.00),
  (2, 'Bob',    500.00);

验证数据

确认行数:

SELECT 'users' AS table_name, COUNT(*) AS rows_count FROM users
UNION ALL SELECT 'products', COUNT(*) FROM products
UNION ALL SELECT 'orders', COUNT(*) FROM orders
UNION ALL SELECT 'order_items', COUNT(*) FROM order_items
UNION ALL SELECT 'accounts', COUNT(*) FROM accounts;

预期:users 8、products 8、orders 10、order_items 15、accounts 2。

再核对订单总额与明细合计是否一致:

SELECT o.id, o.total_amount,
       SUM(oi.quantity * oi.unit_price) AS item_total
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.id
GROUP BY o.id, o.total_amount
ORDER BY o.id;

每一行的 total_amount 与 item_total 应该相等。如果不等,说明种子数据被改过,直接重置。

重置练习库

练习库可以随时删掉重来。确认当前连的是本机 sql_tutorial,不要在生产库执行:

DROP DATABASE IF EXISTS sql_tutorial;

然后重新执行本页的建库、建表和插入脚本。也可以把整页 SQL 保存成 sample.sql,用命令行一次导入:

mysql -h 127.0.0.1 -P 3306 -u root -p < sample.sql

易错点

相关条目

参考链接

读完自测

  1. 为什么先建 users,再建 orders?
  2. orders.total_amount 与 order_items 的合计应该满足什么关系?
  3. 想清空练习库重来,应该用哪条语句?
答案
  1. orders.user_id 有外键指向 users.id,被引用的表必须先存在。
  2. 每个订单的 total_amount 应等于该订单所有 quantity * unit_price 之和。
  3. DROP DATABASE IF EXISTS sql_tutorial;,然后重新执行建库和插入脚本。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令