数据库保护

冲突可串行化判断

简化成读写序列

冲突可串行化判定 –> 前趋图中没有回路

对前趋图做拓扑排序

两段锁协议

并发执行的所有事务都遵循2PL –> 冲突可串行化

sql

-- (1)查询至少参与过两个国家级项目的学生的姓名;

select Sname
from S,P,S_P
where s.sno=s_p.sno and s_p.pno=p.pno and p.level='国家级'
group by s.sno
having count(*)>=2;
-- (2)查询计算机学院没有参与过任何项目的学生学号和姓名;

select s.sno,sname
from s,d
where s.dno=d.dno and d.dname='计算机学院' and s.sno not in(
select distinct sno
from s_p
)
-- (3)查询参与了王平负责的所有项目的学生姓名和联系方式;

王平负责的所有项目 --> 小集合B

s_p --> 大集合A xxx参与的项目

不存在B-A


A:

select pno
from s_p
where sno='xxx'

B:
select pno
from p,s
where p.sno=s.sno and s.sname='王平'


select sno,sname
from s as a
not exists(
(select pno
from p,s as b
where p.sno=b.sno and b.sname='王平')
except(
select pno
from s_p
where s_p.sno=a.sno
)
)

另一种方法

select sno,sname
from s
where not exists(
select *
from p,s as b
where p.sno=b.sno and b.sname='王平' and not exists(
select *
from s_p
where p.pno=s_p.pno ans s_p.sno=s.sno;
)
);
-- (4)对 2017年立项的项目,统计各学院立项项目的平均资助金额;

select d.dno,d.dname,avg(Funding)
from d,p
where d.dno=p.dno and p.year=2017
group by d.dno;

-- (5)建立视图,查询 2015年-2017年间各学院立项的国家级项目的数量,示学院
-- 名称和项目数量

creat view a
as
select d.dname,count(*) as num
from d,p
where d.dno=p.pno and p.year between 2015 and 2017 and p.level='xxx'
group by d.dno