门户网站开发价格,小学校园网站建设要求,做seo是什么意思,像乐视做硬件的视频网站目录 1. 题目1.1 存储过程1.2 存储函数1.3 事务处理 2. 解答2.1 存储过程2.2 存储函数2.3 事务处理 1. 题目
1.1 存储过程 创建表 RandNumber #xff1a;字段#xff1a;id 自增长#xff0c; data int#xff1b; 创建存储过程向表中插入指定个数的随机数#xff08;1-… 目录 1. 题目1.1 存储过程1.2 存储函数1.3 事务处理 2. 解答2.1 存储过程2.2 存储函数2.3 事务处理 1. 题目
1.1 存储过程 创建表 RandNumber 字段id 自增长 data int 创建存储过程向表中插入指定个数的随机数1-99但如果插入的数为 50则终止插入。 创建存储过程根据员工的工作时间如果大于 6 年时将其转到经理办公室工作并调用该存储过程。 创建存储过程比较两个员工的实际收入若前者比后者高输出 1若两者相等输出 0若后者比前者高输出 -1并调用该存储过程。 创建存储过程 p(in name char(10),out income decimal(7,2))计算一个员工的实际收入并调用该存储过程将员工 朱骏 的实际收入保存在一个用户变量中。 创建存储过程 raise(in edu char(6)in x decimal(5,1)) 将所有某种学历的员工的收入提高 %x, 并调用该存储过程将所有硕士学历的员工的收入提高 10%。
1.2 存储函数 创建存储函数 getAver(did int)计算某个部门的平均工资实际收入; 调用该函数显示平均工资最高和最低的部门名称。
1.3 事务处理
设置事务处理为手动提交建立两个连接。 观察 transaction_isolation 设置为 read-uncommited 时脏读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 read-uncommited开始事务显示 employees 表中‘ 王林 ’员工信息 2在另一个连接 B 中修改‘ 王林 ’的 workYear 为 10 年 3在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear 4在一个连接 B 中回滚刚才的修改操作 5在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear。 观察 transaction_isolation 设置为 read-commited 时不可重复读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 read-commited 2开始事务显示 employees 表中‘王林’员工信息 3在另一个连接 B 中修改‘王林’的 workYear 为 10 年 4在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear 5在一个连接 B 中提交刚才的修改操作 6在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear提交事务。 观察 transaction_isolation 设置为 repeatable-read 时幻读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 repeatable-read 2开始事务显示 employees 表中所有员工信息观察记录的数目 3在另一个连接 B 中在 employees 表插入一条记录并提交事务 4在连接 A 中显示 employees 表的员工信息观察记录的数目 5在连接 A 中将所有员工的 workYear 增加一年观察被修改的记录的数目 6在连接 A 中提交事务 8在连接 A 再次显示 employees 表中所有员工信息观察记录的数目 设置transaction_isolation 设置为 serializable 重复第 3 个实验的操作观察操作中出现的现象。
2. 解答
2.1 存储过程
use yggl;创建表 RandNumber 字段id 自增长 data int 创建存储过程向表中插入指定个数的随机数1-99但如果插入的数为 50则终止插入。 drop table if EXISTS yggl.RandNumber;
CREATE TABLE if not EXISTSyggl.RandNumber (id int NOT NULL AUTO_INCREMENT,data int NOT NULL,PRIMARY KEY (id)
);drop PROCEDURE if EXISTS p_RandNumber;
delimiter $
create procedure p_RandNumber(in n int)
begindeclare temp int;declare i int default(1);set temp 1 floor(rand()*99);while i n and temp ! 50 doinsert into randnumber values (null, temp);set temp 1 floor(rand()*99);set i i 1;end while;
end$
delimiter ;set n100;
call p_RandNumber(n);
select * from randnumber;创建存储过程根据员工的工作时间如果大于 6 年时将其转到经理办公室工作并调用该存储过程。 drop PROCEDURE if EXISTS p2;
delimiter $
create procedure p2()
begindeclare did char(3); # 部门编号declare eid char(6); # 员工编号select departments.DepartmentID into didfrom departmentswhere departments.DepartmentName 经理办公室;select employees.EmployeeID into eidfrom employeeswhere employees.WorkYear 6;update employeesset DepartmentID didwhere employees.EmployeeID in(eid);
end$
delimiter ;call p2();创建存储过程比较两个员工的实际收入若前者比后者高输出 1若两者相等输出 0若后者比前者高输出 -1并调用该存储过程。 drop PROCEDURE if EXISTS p3;
delimiter $
create procedure p3(in mname1 char(10), in mname2 char(10))
begindeclare m1 float; # 第一个人的实际收入declare m2 float; # 第二个人的实际收入declare flag int; # 1,0,-1select salary.InCome - salary.OutCome into m1from salary join employees on salary.EmployeeID employees.EmployeeIDwhere employees.Name mname1;select salary.InCome - salary.OutCome into m2from salary join employees on salary.EmployeeID employees.EmployeeIDwhere employees.Name mname2;if m1 m2 thenset flag 1;elseif m1 m2 thenset flag 0;elseset flag -1;end if;select flag;
end$
delimiter ;call p3(王浩, 伍容华);创建存储过程 p(in name char(10),out income decimal(7,2))计算一个员工的实际收入并调用该存储过程将员工 朱骏 的实际收入保存在一个用户变量中。 drop PROCEDURE if EXISTS p;
delimiter $
create procedure p(in name char(10),out income decimal(7,2))
beginselect salary.InCome - salary.OutCome into incomefrom salary join employees on salary.EmployeeID employees.EmployeeIDwhere employees.Name name;end$
delimiter ;set c1;
call p(朱骏, c);
select c;创建存储过程 raise(in edu char(6)in x decimal(5,1)) 将所有某种学历的员工的收入提高 %x, 并调用该存储过程将所有硕士学历的员工的收入提高 10%。 drop PROCEDURE if EXISTS raise;
delimiter $
create procedure raise(in edu char(6), in x decimal(5,1))
beginupdate salaryset salary.InCome salary.InCome*(1x/100)where EmployeeID in(select employees.EmployeeIDfrom employeeswhere employees.Education edu);
end$
delimiter ;call raise(硕士, 10);2.2 存储函数 创建存储函数 getAver(did int)计算某个部门的平均工资实际收入; set GLOBAL log_bin_trust_function_creators 1; # 一共只需要设置一次drop FUNCTION if exists getAver;
delimiter $
create FUNCTION getAver(did int)
returns float # 返回某个部门的平均工资实际收入
begindeclare aver float;select AVG(salary.InCome - salary.OutCome) into averfrom employees join salary on employees.EmployeeID salary.EmployeeIDwhere employees.DepartmentID did;return aver;
end$
delimiter ;调用该函数显示平均工资最高和最低的部门名称。 # 平均工资最高的部门
select departments.DepartmentName, getAver(departments.DepartmentID) as avg_salary
from departments
ORDER BY avg_salary desc
limit 1;
# 平均工资最低的部门
select departments.DepartmentName, getAver(departments.DepartmentID) as avg_salary
from departments
ORDER BY avg_salary asc
limit 1;2.3 事务处理
设置事务处理为手动提交建立两个连接。
set autocommit 0;观察 transaction_isolation 设置为 read-uncommited 时脏读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 read-uncommited开始事务显示 employees 表中‘ 王林 ’员工信息 2在另一个连接 B 中修改‘ 王林 ’的 workYear 为 10 年 3在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear 4在一个连接 B 中回滚刚才的修改操作 5在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear。 结论一个事务 B 读取了另一个未提交的并行事务 A 写的数据。【脏读】 观察 transaction_isolation 设置为 read-commited 时不可重复读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 read-commited 2开始事务显示 employees 表中‘王林’员工信息 3在另一个连接 B 中修改‘王林’的 workYear 为 10 年 4在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear 5在一个连接 B 中提交刚才的修改操作 6在连接 A 中显示 employees 表的员工信息观察‘王林’的 workYear提交事务。 结论一个事务A重新读取前面读取过的数据发现该数据已经被另一个已提交的事务B修改过。【不可重复读】 观察 transaction_isolation 设置为 repeatable-read 时幻读的情况。 1在一个连接 A 中设置 transaction_isolation 设置为 repeatable-read 2开始事务显示 employees 表中所有员工信息观察记录的数目 3在另一个连接 B 中在 employees 表插入一条记录并提交事务 4在连接 A 中显示 employees 表的员工信息观察记录的数目 5在连接 A 中将所有员工的 workYear 增加一年观察被修改的记录的数目 6在连接 A 中提交事务 7在连接 A 再次显示 employees 表中所有员工信息观察记录的数目 结论一个事务重新执行一个查询返回一套符合查询条件的行 发现这些行因为其他最近提交的事务而发生了改变。【幻读】 设置transaction_isolation 设置为 serializable 重复第 3 个实验的操作观察操作中出现的现象即 1在一个连接 A 中设置 transaction_isolation 设置为 serializable 2开始事务显示 employees 表中所有员工信息观察记录的数目 3在另一个连接 B 中在 employees 表插入一条记录并提交事务 4在连接 A 中显示 employees 表的员工信息观察记录的数目 5在连接 A 中将所有员工的 workYear 增加一年观察被修改的记录的数目 6在连接 A 中提交事务 7在连接 A 再次显示 employees 表中所有员工信息观察记录的数目 结论对于同一个数据来说在同一个时间段内只能有一个会话可以访问包括SELECT和DML这样可以避免幻读问题。也就是说对于同一行记录“写”会加“写锁”“读”会加“读锁”。当出现读写锁冲突的时候后访问的事务必须等前一个事务执行完成才能继续执行。【可序列化】
上一篇文章【数据库——MySQL】14过程式对象程序设计——游标、触发器 下一篇文章【数据库——MySQL】16游标和触发器习题及讲解