实验四:数据更新与视图

以SQL Server软件为例;

0.查询student表里的属性

select *
from student;

1.将一个新学生记录
(学号:95020;姓名:陈冬;性别:男;所在系:IS;年龄:18岁)插入到Student表中。)

insert 
into student(sno,sname,ssex,sdept,sage)
values ('95020','陈冬','男','IS',18);

2.将19级所有学生的年龄增加1岁。

update student
set sage = sage+1 
where sno like '2019%';

3.将非软件系全体学生的成绩置零。
备注:先查询非软件系全体学生的成绩,更新完再查询一遍

 

update sc
set score=0 
where sno in (select sno from student where sclass not like '软件工程');


4.  删除2号课程的所有选课记录。
备注:先查询一次,删除后再查询一次 ()删除2号课程的所有选课记录。

select *
from sc
where cno='c2';

delete 
from sc
where cno='c2';

5.删除181软件所有学生的选课记录。
备注:先查询一次,删除后再查询一次

select *
from sc
where sno in
    (    select sno
         from student
         where sclass = '软件工程181';
     );


 delete 
 from sc
 where sno in(select sno from student where sclass = '软件工程181');

6.建立本班级学生的视图。
备注:查询本班学生信息,创建视图,然后做视图查询视图创建 

create view Sclass_student AS
select sno, sname,ssex,sage,sdept,sclass,sbirth,stime
from student
where sclass = '软件工程212';

select *
from Sclass_student;

7.将学生的学号及他的平均成绩定义一个视图。
备注:创建视图,然后做视图查询

create view S_A(sno,score)
as
select sno,avg(score)
from sc
group by sno;

select *
from S_A;

8.在上题的视图中查询平均成绩在90分以上的学生的学号和平均成绩。

select sno,avg(score)
from S_A
group by sno
having avg(score)>90;


9.在软件系学生的视图中找出年龄小于20岁的学生。
备注:创建软件系学生的视图,然后在视图中查询年龄小于20岁的学生

create view ha
as
select *
from student
where sdept like '软件%';

select *
from ha
where sage<20;

10.将软件系20级学生视图在IS_STUDENT中学号为20202218(可以自己给定一个学号)的学生姓名改为刘辰。

create view IS_STUDENT
as
select *
from student
where sclass like '软件%20%';

update IS_STUDENT
set sname = '刘辰'
where sno = '20202218';

11.删除上题创建的视图

delete IS_STUDENT;

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值