zoukankan      html  css  js  c++  java
  • 数据库复习实验

    实验三

    3.1  数据定义(p79三个表)

    基本表的创建、修改及删除

    3.2  数据操作(p79三个表)

    1. 完成各类更新操作(插入数据,修改数据,删除数据)。
    2. 完成简单的查询操作

    实验四

    1. 根据以下要求认真填写实验报告,记录所有的实验用例。

    给表增加一个列

    更改一个列的类型、宽度

    更改一个列的名称

    给列增加一个check约束(如:性别只能取男女)

    删除一个列

    删除主键

    创建主键

    更改一个表的名称

    –增加一列sAddress
    alter table student add sAddress varchar(100)
    select * from student

    –更改列宽度
    alter table student alter column sAddress varchar(50)

    –更改列类型
    alter table student alter column sAddress char(100)

    –更改列名
    sp_rename ‘student.sAddress’ , addr ,’column’
    select * from student

    –给列增加一个check约束(如:性别只能取男女)
    alter table student add constraint ck_student_sAge check(sAge>15 and sAge<30)
    select * from student
    –删除一个列
    alter table student drop column addr
    select * from student

    –创建主键
    alter table student add constraint pk_student_sNo primary Key (sNo)

    –删除主键
    alter table stu drop constraint pk_student_sNo

    –更改表名
    exec sp_rename ‘student’, ‘stu’;

    实验五

    1. 给表student列sno增加检查长度为8位的约束并测试。
    2. 给表student列ssex的输入限定为男、女两个值并测试。
    3. 给表sc列grade的输入限定为0到100并测试。
    4. 给表sc的列增加外键约束并测试。
    5. 给表student增加列idcard表示身份证号并限定输入长度为18位,且第17位奇数表示男,偶数表示女,这个值必须与ssex一致,并请测试。

    –1.查询姓名为“王华”的学号、性别、年龄、院系;
    insert into student values(‘19150221′,’王华’,’男’,20,’计算机’)
    select sNo,sSex,sAge,sDep from student where sName=’王华’

    –2. 查询名字中第2个字为”阳”字的学生的姓名和学号。
    insert into student values(‘19150222′,’王阳华’,’男’,20,’计算机’)
    select sName,sNo from student where sName like ‘_阳%’

    实验六

    1.查询姓名为“王华”的学号、性别、年龄、院系;

    1. 查询名字中第2个字为”阳”字的学生的姓名和学号。

    3.查询所有不姓刘的学生姓名、学号和性别。

    4.查询以”DB_”开头,且倒数第3个字符为 i的课程的详细情况。

    –1.查询姓名为“王华”的学号、性别、年龄、院系;
    insert into student values(‘19150221′,’王华’,’男’,20,’计算机’)
    select sNo,sSex,sAge,sDep from student where sName=’王华’

    –2. 查询名字中第2个字为”阳”字的学生的姓名和学号。
    insert into student values(‘19150222′,’王阳华’,’男’,20,’计算机’)
    select sName,sNo from student where sName like ‘_阳%’

    –3.查询所有不姓刘的学生姓名、学号和性别。
    insert into student values(‘19150223′,’刘某某’,’男’,20,’计算机’)
    select sName,sNo,sSex from student where sName not like ‘刘%’

    –4.查询以”DB_”开头,且倒数第3个字符为 i的课程的详细情况。
    create table course (
    cNo varchar(50) primary key,
    cName varchar(50)
    )
    insert into course values(‘001′,’数据库’)
    insert into course values(‘002′,’DB_abciab’)
    select * from course where cName like ‘DB_%i__’

    实验七

    1. 创建视图v_1查询计算机系的所有学生学号、姓名、性别、年龄。

    2.查询计算机系的学生人数。

    3.列出每门课程的均分、最高分数、最低分数、选课人数。

    –1. 创建视图v_1查询计算机系的所有学生学号、姓名、性别、年龄。
    create view v_1 as select sNo,sName,sSex,sAge,sDept from student where sDept=’计算机’
    select * from v_1

    –2.查询计算机系的学生人数。
    select COUNT(*) as num from student where sDept=’计算机’

    –3.列出每门课程的均分、最高分数、最低分数、选课人数。

    select cNo,AVG(grade) as avgGrade,MAX(grade) as maxGrade,MIN(grade) as minGrade,COUNT(*) as sNum
    from sc group by cNo

    实验八

    1. 查询每一门课的间接先修课(即先修课的先修课)
    2. 查询每个学生的学号、姓名、选修的课程名及成绩
    3. 查询与“刘晨”在同一个系学习的学生。
    4. 查询选修了课程名为“信息系统”的学生学号和姓名
    5. 找出每个学生超过他选修课程平均成绩的课程号与课程名。

    –1.查询每一门课的间接先修课(即先修课的先修课)
    select a.cNo,b.cPno as pnoOfpno from course a,course b where a.cPno=b.cNo

    –2. 查询每个学生的学号、姓名、选修的课程名及成绩

    select a.sNo,a.sName,b.cName,c.grade from student a,course b,sc c
    where a.sNo=c.sNo and b.cNo=c.cNo

    –3. 查询与“刘晨”在同一个系学习的学生。
    select student.sNo,student.sName from student
    where sDept in(select sDept from student where sName=’刘晨’)

    –4. 查询选修了课程名为“信息系统”的学生学号和姓名
    insert into course values(‘007′,’信息系统’,’006′)
    insert into sc values(‘19150203′,’007′,93)

    select a.sNo,a.sName from student a,sc b ,course c
    where a.sNo in (select b.sNo from sc where b.cNo in
    (select c.cNo from course where c.cName=’信息系统’))

    –5. 找出每个学生超过他选修课程平均成绩的课程号与课程名。

    实验十

    1. 1列出每个系的男生人数、女生人数
    2. 查询选修了课程名为“信息系统”的学生学号和姓名
    3. 删除“王兰”所选的全部课程;
    4. 将“计算机”系的学生成绩全部清零;
    5. 查询没有选修1号课程的学生姓名。
    6. 查询选修了课程1或者选修了课程2的学生姓名。
    7. 查询既选修了课程1又选修了课程2 的学生姓名
    8. 查询选修了课程1但没有又选修了课程2的学生姓名

    –1. 1列出每个系的男生人数、女生人数
    select sDept,COUNT(case when sSex=’男’ then sSex end),COUNT(case when sSex=’女’ then sSex end)
    from student
    group by sDept

    select sDept ,case sSex when ‘男’ then COUNT(sSex) when ‘女’ then COUNT(sSex)
    from student group by sDept

    –2. 查询选修了课程名为“信息系统”的学生学号和姓名
    select a.sNo,a.sName
    from student a,course b,sc c
    where a.sNo=c.sNo and
    b.cNo=c.cNo and
    b.cName=’信息系统’

    –3. 删除“王兰”所选的全部课程;
    delete from sc
    where sNo in
    (select sNo from student where sName=’王兰’)

    –4. 将“计算机”系的学生成绩全部清零;
    update sc set grade=0
    where sNo in
    (
    select sNo from student
    where sDept=’计算机’
    )

    –5. 查询没有选修1号课程的学生姓名。
    select sName
    from student

    except

    select a.sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’001′

    –6. 查询选修了课程1或者选修了课程2的学生姓名。
    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’001′
    union

    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’002′

    –7. 查询既选修了课程1又选修了课程2 的学生姓名
    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’001′

    intersect

    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’002′

    –8. 查询选修了课程1但没有又选修了课程2的学生姓名
    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’001′

    except

    select sName
    from student a,sc b
    where a.sNo=b.sNo and
    b.cNo=’002′

    实验十一

    1. 创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩
    2. 查询平均成绩在60分以上的同学的学号、姓名;
    3. 查询平均成绩在全系平均成绩之上的同学的学号、姓名;

    –1.创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩
    create view V1 as
    select a.sNo,a.sName,b.grade as ‘数据结构’,c.grade as ‘数据库原理’
    from student a left outer join sc b
    on a.sNo=b.sNo and b.cNo in (select cNo from course where cName=’数据结构’)
    left outer join sc c
    on a.sNo=c.sNo and c.cNo in (select cNo from course where cName=’数据库原理’)

    select * from V1

    –2.查询平均成绩在60分以上的同学的学号、姓名;
    select a.sNo,a.sName
    from student a,sc b
    where a.sNo=b.sNo
    group by a.sNo,a.sName
    having AVG(grade)>60

    –3.查询平均成绩在 全系平均成绩之上的同学的学号、姓名;
    select a.sNo,a.sName
    from student a,sc b
    where a.sNo=b.sNo
    group by a.sNo,a.sName
    having AVG(b.grade)>(

    select AVG(c.grade) from sc c
    where c.sNo in (select d.sNo from student d where d.sDept=a.sDept)

    )

    实验十二

    1. 查询选修课程包含学号为19120302的学生所选课程的学生学号、姓名。
    2. 列出与学号19120302同学选修课程相同的同学的学号、姓名。

    –1.查询选修课程 包含 学号为19150302的学生所选课程的学生学号、姓名。

    select a.sno,a.sname
    from student a
    where
    (
    select COUNT(*) from
    (
    (select cno from sc where sNo=a.sno)
    intersect
    (select cno from sc where sNo=’19150302′)
    )b
    )=(select COUNT(*) from sc where sNo=’19150302′)
    and a.sNo!=’19150302′
    –2. 列出与学号19150302同学选修课程 相同 的同学的学号、姓名。

    select a.sNo,a.sName
    from student a
    where
    (select COUNT(*) from sc where sNo=a.sno)=
    (select COUNT(*) from sc where sNo=’19150302′)
    and a.sno not in
    (
    select sno from sc where cNo not in(
    select cNo from sc where sNo=’19150302′
    )
    )
    and a.sNo!=’19150302′

  • 相关阅读:
    程序员是这样区分Null和Undefined
    JavaScript实现
    获取页面中任意一个元素距离body的偏移量
    js检测数据类型的方法你都掌握了几个?
    处理浏览器兼容你最喜欢用哪种方式
    算法竞赛入门经典 第四章 学习笔记 1
    算法竞赛入门经典 程序4-1 组合数
    算法竞赛入门经典 例题3-1 TeX中的引号
    算法竞赛入门经典 第3章 数组和字符串 学习笔记 3
    算法竞赛入门经典 第3章 数组和字符串 学习笔记 2
  • 原文地址:https://www.cnblogs.com/xiu68/p/8335154.html
Copyright © 2011-2022 走看看