当前位置: 代码网 > it编程>数据库>Mysql > MySQL多表查询、事务与索引的实践与应用操作

MySQL多表查询、事务与索引的实践与应用操作

2025年04月12日 Mysql 我要评论
摘要:本文围绕mysql数据库操作展开,通过构建部门与员工管理、餐饮业务相关的数据库表,并填充测试数据,系统地阐述了多表查询的多种方式,包括内连接、外连接和不同类型的子查询,同时介绍了事务的处理以及索

摘要:本文围绕mysql数据库操作展开,通过构建部门与员工管理、餐饮业务相关的数据库表,并填充测试数据,系统地阐述了多表查询的多种方式,包括内连接、外连接和不同类型的子查询,同时介绍了事务的处理以及索引的创建、查询和删除操作。

关键词:mysql;多表查询;事务;索引

一、引言

在数据库管理与开发过程中,多表查询、事务管理以及索引优化是提升数据处理效率和数据完整性的关键技术。本文通过实际案例详细展示这些技术在mysql数据库中的具体应用。

二、数据准备

2.1 部门与员工表的创建及数据插入

部门表(tb_dept):用于存储部门相关信息。

create table tb_dept(
    id int unsigned primary key auto_increment comment '主键id',
    name varchar(10) not null unique comment '部门名称',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
) comment '部门表';
insert into tb_dept (id, name, create_time, update_time) values
(1, '学工部', now(), now()),
(2, '教研部', now(), now()),
(3, '咨询部', now(), now()),
(4, '就业部', now(), now()),
(5, '人事部', now(), now());

工表(tb_emp):通过 dept_id 与部门表关联,记录员工详细信息。

create table tb_emp (
    id int unsigned primary key auto_increment comment 'id',
    username varchar(20) not null unique comment '用户名',
    password varchar(32) default '123456' comment '密码',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
    image varchar(300) comment '图像',
    job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
    entrydate date comment '入职时间',
    dept_id int unsigned comment '部门id',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
) comment '员工表';
insert into tb_emp(id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) values
(1, 'jinyong', '123456', '金庸', 1, '1.jpg', 4, '2000 - 01 - 01', 2, now(), now()),
(2, 'zhangwuji', '123456', '张无忌', 1, '2.jpg', 2, '2015 - 01 - 01', 2, now(), now()),
-- 省略部分插入数据
(17, 'chenyouliang', '123456', '陈友谅', 1, '17.jpg', null, '2015 - 03 - 21', null, now(), now());

2.2 餐饮业务相关表的创建及数据插入

分类表(category):区分菜品分类与套餐分类。

create table category(
    id int unsigned primary key auto_increment comment '主键id',
    name varchar(20) not null unique comment '分类名称',
    type tinyint unsigned not null comment '类型 1 菜品分类 2 套餐分类',
    sort tinyint unsigned not null comment '顺序',
    status tinyint unsigned not null default 0 comment '状态 0 禁用,1 启用',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '更新时间'
) comment '分类';

菜品表(dish):记录菜品的各项属性,与分类表通过 category_id 关联。

create table dish(
    id int unsigned primary key auto_increment comment '主键id',
    name varchar(20) not null unique comment '菜品名称',
    category_id int unsigned not null comment '菜品分类id',
    price decimal(8, 2) not null comment '菜品价格',
    image varchar(300) not null comment '菜品图片',
    description varchar(200) comment '描述信息',
    status tinyint unsigned not null default 0 comment '状态, 0 停售 1 起售',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '更新时间'
) comment '菜品';

套餐表(setmeal):存储套餐信息,与分类表通过 category_id 关联。

create table setmeal(
    id int unsigned primary key auto_increment comment '主键id',
    name varchar(20) not null unique comment '套餐名称',
    category_id int unsigned not null comment '分类id',
    price decimal(8, 2) not null comment '套餐价格',
    image varchar(300) not null comment '图片',
    description varchar(200) comment '描述信息',
    status tinyint unsigned not null default 0 comment '状态 0:停用 1:启用',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '更新时间'
) comment '套餐';

套餐菜品关联表(setmeal_dish):建立套餐与菜品之间的联系。

create table setmeal_dish(
    id int unsigned primary key auto_increment comment '主键id',
    setmeal_id int unsigned not null comment '套餐id ',
    dish_id int unsigned not null comment '菜品id',
    copies tinyint unsigned not null comment '份数'
) comment '套餐菜品中间表';

插入测试数据:向上述餐饮业务相关表插入大量测试数据,涵盖各类菜品、套餐及其关联信息。

三、多表查询操作

3.1 基本多表查询

通过连接 tb_emptb_dept 表,获取员工所属部门信息。

select * from tb_emp, tb_dept where tb_emp.dept_id = tb_dept.id;

3.2 内连接

隐式内连接:查询员工姓名及所属部门名称,可通过起别名提高可读性。

select tb_emp.name, tb_dept.name from tb_emp, tb_dept where tb_emp.dept_id = tb_dept.id;
select e.name, d.name from tb_emp e, tb_dept d where e.dept_id = d.id;

显式内连接:同样实现查询员工姓名及所属部门名称。

select tb_emp.name, tb_dept.name from tb_emp join tb_dept on tb_emp.dept_id = tb_dept.id;

3.3 外连接

左外连接:获取员工表所有员工姓名及对应的部门名称,包括无部门员工。

select e.name, d.name from tb_emp e left join tb_dept d on e.dept_id = d.id;

右外连接:获取部门表所有部门名称及对应的员工名称,包括无员工部门。

select e.name, d.name from tb_emp e right join tb_dept d on e.dept_id = d.id;
-- 等同于
select e.name, d.name from tb_dept d left join tb_emp e on e.dept_id = d.id;

3.4 子查询

标量子查询

查询“教研部”的所有员工信息,先获取教研部 id,再查询该部门员工。

select id from tb_dept where name = '教研部';
select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');

- 查询在“方东白”入职之后的员工信息,先获取方东白入职时间,再查询晚于该时间入职的员工。

select entrydate from tb_emp where name = '方东白';
select * from tb_emp where entrydate > (select entrydate from tb_emp where name = '方东白');

列子查询:查询“教研部”和“咨询部”的所有员工信息,先获取两个部门 id,再查询对应部门员工。

select id from tb_dept where name = '教研部' or name = '咨询部';
select * from tb_emp where dept_id in (select id from tb_dept where name = '教研部' or name = '咨询部');

行子查询:查询与“韦一笑”入职日期及职位都相同的员工信息,可通过两种方式实现。

select entrydate, job from tb_emp where name = '韦一笑';
-- 方式一
select * from tb_emp where entrydate = (select entrydate from tb_emp where name = '韦一笑') and job = (select job from tb_emp where name = '韦一笑');
-- 方式二
select * from tb_emp where (entrydate, job) = (select entrydate, job from tb_emp where name = '韦一笑');

表子查询:查询入职日期在“2006 - 01 - 01”之后的员工信息及其部门名称,先获取符合日期条件的员工,再连接部门表获取部门名称。

select * from tb_emp where entrydate > '2006 - 01 - 01';
select e.*, d.name from (select * from tb_emp where entrydate > '2006 - 01 - 01') e, tb_dept d where e.dept_id = d.id;

3.5 餐饮业务多表查询需求

查询低价菜品信息:获取价格低于10元的菜品名称、价格及分类名称。

select d.name, d.price, c.name
from dish d, category c
where d.category_id = c.id and d.price < 10;

查询特定价格与状态菜品信息:查询价格在10元(含)到50元(含)之间且状态为“起售”的菜品信息,包括无分类菜品。

select d.name, d.price, c.name
from dish d left join category c on d.category_id = c.id
where d.price between 10 and 50 and d.status = 1;

查询各分类最贵菜品信息:展示每个分类下最贵菜品的分类名称和价格。

select c.name, max(d.price)
from dish d, category c
where d.category_id = c.id
group by c.name;

查询特定条件分类名称:获取菜品状态为“起售”且菜品数量大于等于3的分类名称。

select c.name, count(*)
from dish d, category c
where d.category_id = c.id and d.status = 1
group by c.name
having count(*) >= 3;

查询套餐包含菜品信息:展示“商务套餐a”包含的菜品相关信息。

select s.name, s.price, d.name, d.price, sd.copies
from setmeal s, setmeal_dish sd, dish d
where s.id = sd.setmeal_id and sd.dish_id = d.id and s.name = '商务套餐a';

查询低于平均价格菜品信息:先计算菜品平均价格,再查询低于该平均价格的菜品。

select avg(price) from dish;
select * from dish where price < (select avg(price) from dish);

四、事务操作

4.1 事务处理流程

在删除部门及相关员工操作中,使用事务确保数据一致性。

-- 开启事务
start transaction;
-- 删除部门
delete from tb_dept where id = 2;
-- 删除部门下的员工
delete from tb_emp where dept_id = 2;
-- 提交事务
commit;
-- 回滚事务(若中途出错)
rollback;
select * from tb_dept;
select * from tb_emp;

五、索引操作

5.1 索引的创建、查询与删除

创建索引:为 tb_sku 表的 sn 字段和 tb_emp 表的 name 字段创建索引。

create index idx_sku_sn on tb_sku(sn);
create index idx_emp_name on tb_emp(name);

查询索引信息:查看 tb_emp 表的索引情况。

show index from tb_emp;

删除索引:删除 tb_emp 表中 name 字段的索引。

drop index idx_emp_name on tb_emp;

到此这篇关于mysql多表查询、事务与索引的实践与应用的文章就介绍到这了,更多相关mysql多表查询、事务内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com