测试用数据
视频教程
TBD
表说明
test_user: 测试用户主数据表,存储系统用户基本信息。test_order: 测试订单主表,每个订单归属于一个用户,支持按租户隔离。test_order_item: 测试订单明细表,记录订单中每一项商品信息,与订单为一对多关系。test_points_record: 测试积分流水表,记录用户积分变动;与用户为一对多关系,订单与积分记录为一对一关系(通过order_id字段关联)。
ER 图
表定义SQL
sql
-- 删除已存在的测试表
DROP TABLE IF EXISTS test_order_item;
DROP TABLE IF EXISTS test_points_record;
DROP TABLE IF EXISTS test_order;
DROP TABLE IF EXISTS test_user;
-- 用户表
CREATE TABLE test_user (
id BIGINT AUTO_INCREMENT COMMENT '主键' PRIMARY KEY,
create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
deleted BIT DEFAULT b'0' NOT NULL COMMENT '是否删除',
tenant_id BIGINT DEFAULT 0 NOT NULL COMMENT '租户编号',
username VARCHAR(64) DEFAULT '' NOT NULL COMMENT '用户名',
nickname VARCHAR(64) DEFAULT '' NOT NULL COMMENT '昵称',
email VARCHAR(128) DEFAULT '' NOT NULL COMMENT '邮箱'
) COMMENT '测试-用户表' COLLATE = utf8mb4_unicode_ci;
CREATE INDEX idx_test_user_username ON test_user(username);
CREATE INDEX idx_test_user_tenant_id ON test_user(tenant_id);
-- 订单表(主表):一个用户可拥有多个订单
CREATE TABLE test_order (
id BIGINT AUTO_INCREMENT COMMENT '主键' PRIMARY KEY,
user_id BIGINT NOT NULL COMMENT '用户ID',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
deleted BIT DEFAULT b'0' NOT NULL COMMENT '是否删除',
tenant_id BIGINT DEFAULT 0 NOT NULL COMMENT '租户编号',
order_no VARCHAR(64) DEFAULT '' NOT NULL COMMENT '订单编号',
total_amount DECIMAL(12,2) DEFAULT 0.00 NOT NULL COMMENT '订单总金额'
) COMMENT '测试-订单表' COLLATE = utf8mb4_unicode_ci;
CREATE INDEX idx_test_order_user_id ON test_order(user_id);
CREATE INDEX idx_test_order_order_no ON test_order(order_no);
CREATE INDEX idx_test_order_tenant_id ON test_order(tenant_id);
-- 订单明细表(副表):一个订单包含多个明细项
CREATE TABLE test_order_item (
id BIGINT AUTO_INCREMENT COMMENT '主键' PRIMARY KEY,
order_id BIGINT NOT NULL COMMENT '订单ID',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
deleted BIT DEFAULT b'0' NOT NULL COMMENT '是否删除',
tenant_id BIGINT DEFAULT 0 NOT NULL COMMENT '租户编号',
item_name VARCHAR(255) DEFAULT '' NOT NULL COMMENT '商品名称',
quantity INT DEFAULT 0 NOT NULL COMMENT '数量',
unit_price DECIMAL(10,2) DEFAULT 0.00 NOT NULL COMMENT '单价'
) COMMENT '测试-订单明细表' COLLATE = utf8mb4_unicode_ci;
CREATE INDEX idx_test_order_item_order_id ON test_order_item(order_id);
CREATE INDEX idx_test_order_item_tenant_id ON test_order_item(tenant_id);
-- 积分明细表(副表):一个用户有多条积分记录,每笔订单最多对应一条积分记录
CREATE TABLE test_points_record (
id BIGINT AUTO_INCREMENT COMMENT '主键' PRIMARY KEY,
user_id BIGINT NOT NULL COMMENT '用户ID',
order_id BIGINT DEFAULT 0 NOT NULL COMMENT '关联订单ID,0表示无订单',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
deleted BIT DEFAULT b'0' NOT NULL COMMENT '是否删除',
tenant_id BIGINT DEFAULT 0 NOT NULL COMMENT '租户编号',
points_change INT DEFAULT 0 NOT NULL COMMENT '积分变动值',
reason VARCHAR(255) DEFAULT '' NOT NULL COMMENT '变动原因'
) COMMENT '测试-积分明细表' COLLATE = utf8mb4_unicode_ci;
CREATE INDEX idx_test_points_record_user_id ON test_points_record(user_id);
CREATE UNIQUE INDEX uk_test_points_record_order_id ON test_points_record(order_id);
CREATE INDEX idx_test_points_record_tenant_id ON test_points_record(tenant_id);测试数据SQL
- test_user: 3 条用户,租户 ID 为 1,含用户名、中文昵称和邮箱。
- test_order: 3 条订单,分别属于用户 1 和 2,租户 ID 为 1,订单编号唯一,金额合理。
- test_order_item: 7 条明细,每个订单至少含 2 个商品,租户 ID 为 1,数量与单价总和匹配对应订单总金额。
- test_points_record: 3 条积分记录,一一对应 3 个订单,租户 ID 为 1,积分值为正,原因明确。
sql
-- 插入测试用户数据
INSERT INTO test_user (tenant_id, username, nickname, email)
VALUES (1, 'zhangsan', '张三', 'zhangsan@example.com'),
(1, 'lisi', '李四', 'lisi@example.com'),
(1, 'wangwu', '王五', 'wangwu@example.com');
INSERT INTO test_order (tenant_id, user_id, order_no, total_amount)
VALUES (1, 1, 'ORD20251115001', 399.98),
(1, 1, 'ORD20251115002', 225.75),
(1, 2, 'ORD20251115003', 1298.00);
INSERT INTO test_order_item (tenant_id, order_id, item_name, quantity, unit_price)
VALUES (1, 1, '华为手机', 1, 299.99),
(1, 1, '手机壳', 2, 49.99),
(1, 2, '小米手环', 1, 75.25),
(1, 2, '充电线', 3, 50.17),
(1, 3, '苹果笔记本', 1, 899.00),
(1, 3, '无线鼠标', 1, 199.00),
(1, 3, '键盘膜', 2, 100.00);
INSERT INTO test_points_record (tenant_id, user_id, order_id, points_change, reason)
VALUES (1, 1, 1, 40, '下单获得积分'),
(1, 1, 2, 23, '下单获得积分'),
(1, 2, 3, 130, '下单获得积分');