--1. 建立选修表的备份表SC1。
select * into SC1 from SC
--2. 向课程表添加一条记录“121”,“.Net开发”,“考查”,2,“95012”。
insert into C values('121','.NET开发','考查',2,'95012')
--3. 将SC1中选修了110课程,成绩小于70分的都改为70。
update SC1 set score=70
where Cid='110' and score<70
--4. 删除SC1表,将选修了课程,score小于70分的记录都删掉。
delete SC1 where Cid='101' and score<70
--5.要求将性别是女且平均成绩大于80分的学生编号,平均成绩插入S_SCORE表。
select Sid,score as 'avgscore' into S_SCORE from SC where 1<>1
go
insert into S_SCORE
select S.Sid,avg(score)
from S,SC
where S.Sid=SC.Sid and sex='女'
group by S.Sid
having avg(score)>80
--6.将选修了‘104’课程且小于该课程平均成绩的成绩元组从SC1中删除。
delete SC1 where Cid='104' and score<(
select avg(score) from SC1 where Cid='104'
)
--7.把女同学的score提高10%
update SC1 set score=score*1.1 where Sid in(
select Sid from S where sex='女'
)
update SC1 set score=score*1.1 where exists(
select * from S where sex='女' and S.Sid=SC1.Sid
)
--8. 对于SC1表,把选修了“java语言”且成绩低于该门课程平均成绩时的学生分数提高5%。
update SC1 set score=score*1.05 where Cid=(
select Cid from C where cname='Java语言')
and score<(
select avg(score) from SC where Cid=(
select Cid from C where cname='Java语言')
)
declare @cid char(3)
select @cid=Cid from C where cname='Java语言'
select @cid
print @cid
update SC1 set score=score*1.05 where Cid=@cid
and score<(
select avg(score) from SC where Cid=@cid
)
--9. 将计算机系所有教师信息插入到表“教师表1”中。
select * into 教师表1 from T where T.department='计算机系'
select * into 教师表2 from T where 1<>1
insert into 教师表2
select * from T where T.department='计算机系'
--10. 将叶飞同学所选的汇编语言课程成绩提高5分。(用两种方法)
update SC1 set score=score+5
where SC1.Cid=(select cid from C where C.cname='Java语言')
and SC1.Sid=(select Sid from S where S.sname='叶飞')
update SC1 set score=score+5
where exists
(select Sid from S where S.sname='叶飞' and S.Sid=SC1.Sid) and exists(
select * from C where C.cname='Java语言' and C.Cid=SC1.Cid)