• MySQL4


    目录

    多表查询练习

    事务

    事务简介

    事务操作

    事务四大特性

    并发事务问题

    事务隔离级别


    多表查询练习

    1. -- ---------------------------------------> 多表查询案例 <----------------------------------
    2. create table salgrade(
    3. grade int,
    4. losal int,
    5. hisal int
    6. ) comment '薪资等级表';
    7. insert into salgrade values (1,0,3000);
    8. insert into salgrade values (2,3001,5000);
    9. insert into salgrade values (3,5001,8000);
    10. insert into salgrade values (4,8001,10000);
    11. insert into salgrade values (5,10001,15000);
    12. insert into salgrade values (6,15001,20000);
    13. insert into salgrade values (7,20001,25000);
    14. insert into salgrade values (8,25001,30000);
    15. 1. 查询员工的姓名、年龄、职位、部门信息 (隐式内连接)
    16. -- 表: emp , dept
    17. -- 连接条件: emp.dept_id = dept.id
    18. select e.name , e.age , e.job , d.name from emp e , dept d where e.dept_id = d.id;
    19. 2. 查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)
    20. -- 表: emp , dept
    21. -- 连接条件: emp.dept_id = dept.id
    22. select e.name , e.age , e.job , d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30;
    23. 3. 查询拥有员工的部门ID、部门名称
    24. -- 表: emp , dept
    25. -- 连接条件: emp.dept_id = dept.id
    26. select distinct d.id , d.name from emp e , dept d where e.dept_id = d.id;
    27. 4. 查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出来
    28. -- 表: emp , dept
    29. -- 连接条件: emp.dept_id = dept.id
    30. -- 外连接
    31. select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age > 40 ;
    32. 5. 查询所有员工的工资等级
    33. -- 表: emp , salgrade
    34. -- 连接条件 : emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal
    35. select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary >= s.losal and e.salary <= s.hisal;
    36. select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary between s.losal and s.hisal;
    37. 6. 查询 "研发部" 所有员工的信息及 工资等级
    38. -- 表: emp , salgrade , dept
    39. -- 连接条件 : emp.salary between salgrade.losal and salgrade.hisal , emp.dept_id = dept.id
    40. -- 查询条件 : dept.name = '研发部'
    41. select e.* , s.grade from emp e , dept d , salgrade s where e.dept_id = d.id and ( e.salary between s.losal and s.hisal ) and d.name = '研发部';
    42. 7. 查询 "研发部" 员工的平均工资
    43. -- 表: emp , dept
    44. -- 连接条件 : emp.dept_id = dept.id
    45. select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发部';
    46. 8. 查询工资比 "灭绝" 高的员工信息。
    47. -- a. 查询 "灭绝" 的薪资
    48. select salary from emp where name = '灭绝';
    49. -- b. 查询比她工资高的员工数据
    50. select * from emp where salary > ( select salary from emp where name = '灭绝' );
    51. 9. 查询比平均薪资高的员工信息
    52. -- a. 查询员工的平均薪资
    53. select avg(salary) from emp;
    54. -- b. 查询比平均薪资高的员工信息
    55. select * from emp where salary > ( select avg(salary) from emp );
    56. 10. 查询低于本部门平均工资的员工信息
    57. -- a. 查询指定部门平均薪资 1
    58. select avg(e1.salary) from emp e1 where e1.dept_id = 1;
    59. select avg(e1.salary) from emp e1 where e1.dept_id = 2;
    60. -- b. 查询低于本部门平均工资的员工信息
    61. select * from emp e2 where e2.salary < ( select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id );
    62. 11. 查询所有的部门信息, 并统计部门的员工人数
    63. select d.id, d.name , ( select count(*) from emp e where e.dept_id = d.id ) '人数' from dept d;
    64. select count(*) from emp where dept_id = 1;
    65. 12. 查询所有学生的选课情况, 展示出学生名称, 学号, 课程名称
    66. -- 表: student , course , student_course
    67. -- 连接条件: student.id = student_course.studentid , course.id = student_course.courseid
    68. select s.name , s.no , c.name from student s , student_course sc , course c where s.id = sc.studentid and sc.courseid = c.id ;

    事务

    事务简介

    事务是一组操作的集合,它是一个不可分割的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败

    事务操作

    方式一

    • 查看/设置事务提交方式
    • SELECT @@autocommit ;   SET @@autocommit = 0 ;
    • 提交事务      COMMIT ;
    • 回滚事务   ROLLBACK ;

    方式二

    • 开启事务      START TRANSACTION或 BEGIN ;
    • 提交事务      COMMIT ;
    • 回滚事务      ROLLBACK ; 
    1. -- ---------------------------- 事务操作 ----------------------------
    2. -- 数据准备
    3. create table account(
    4. id int auto_increment primary key comment '主键ID',
    5. name varchar(10) comment '姓名',
    6. money int comment '余额'
    7. ) comment '账户表';
    8. insert into account(id, name, money) VALUES (null,'张三',2000),(null,'李四',2000);
    9. -- 恢复数据
    10. update account set money = 2000 where name = '张三' or name = '李四';
    11. select @@autocommit;
    12. set @@autocommit = 0; -- 设置为手动提交
    13. 转账操作 (张三给李四转账1000)
    14. -- 1. 查询张三账户余额
    15. select * from account where name = '张三';
    16. -- 2. 将张三账户余额-1000
    17. update account set money = money - 1000 where name = '张三';
    18. 程序执行报错 ...
    19. -- 3. 将李四账户余额+1000
    20. update account set money = money + 1000 where name = '李四';
    21. -- 提交事务
    22. commit;
    23. -- 回滚事务
    24. rollback ;
    25. 方式二
    26. -- 转账操作 (张三给李四转账1000)
    27. start transaction ;
    28. -- 1. 查询张三账户余额
    29. select * from account where name = '张三';
    30. -- 2. 将张三账户余额-1000
    31. update account set money = money - 1000 where name = '张三';
    32. 程序执行报错 ...
    33. -- 3. 将李四账户余额+1000
    34. update account set money = money + 1000 where name = '李四';
    35. -- 提交事务
    36. commit;
    37. -- 回滚事务
    38. rollback;

    事务四大特性

    • 原子性(Atomicity):事务是不可分割的最小操作单元,要么全部成功,要么全部失败。
    • 一致性(Consistency)︰事务完成时,必须使所有的数据都保持一致状态。
    • 隔离性(Ilsolation):数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行。
    • 持久性(Durability):事务一旦提交或回滚,它对数据库中的数据的改变就是永久的。
       

    并发事务问题

    事务隔离级别

     

    1. -- 查看事务隔离级别
    2. select @@transaction_isolation;
    3. -- 设置事务隔离级别
    4. set session transaction isolation level read uncommitted ;
    5. set session transaction isolation level repeatable read ;

  • 相关阅读:
    python selenium下载一个合适的chromedriver.exe(稳定版本)
    EXCEL会计记账报表财务软件企业公司做账系统凭证自动生成报表
    FixedThreadPool
    Flutter中GetX系列三--Dialog使用详情(中间弹框)
    牛客: BM5 合并k个已排序的链表
    Hive 数据仓库介绍
    【性能测试】Cannot assign requested address (Address not available)
    Jenkins: 配合docker来部署项目
    【wpf】深度解析,Bingding是如何寻找数据源的 上篇
    【微软】【ICLR 2022】TAPEX:通过学习神经 SQL 执行器进行表预训练
  • 原文地址:https://blog.csdn.net/m0_66506641/article/details/126081981