数据库原理与应用-期末考试复习题(1)(1) 下载本文

字段名 lx mc ms

字段类型 CHAR CHAR VARCHAR 字段宽度 4 8 60 说明 缺勤类型,主码 缺勤名称 缺勤描述 请用SQL语句完成以下操作:

1.查询每个职工的职工号、姓名、缺勤时间、缺勤天数和缺勤类型信息。

select * from JBQK

2.查询职工号为001的职工的姓名和缺勤天数。

select xm,ts from

JBQK where zgh='001'

3.查询所有姓“张”的职工的职工号、缺勤天数。

select zgh,ts from JBQK where xm like '张%'

4.找出所有缺勤天数在2~3天的职工号和缺勤名称。

select zgh,mc from JBQK,QQLX where JBQK.lx=QQLX.lx

and ts between 2 and 3

5.查询缺勤名称为“病假”的职工的职工号和姓名。 select zgh,xm from JBQK where lx='病假' 6.查询缺勤天数超过平均缺勤天数的职工的职工号和姓名。

select zgh,xm from JBQK where ts>(select avg(ts) from

JBQK)

7.求各缺勤类别的人数。

select count(*) from JBQK group by lx

8.查询在职工基本情况表中没有出现过的缺勤类型及缺勤名称。 select

QQLX.lx,mc

from

JBQK,QQLX

where

JBQK.lx=QQLX.lx and not in(select lx,mc from JBQK) 9.使用SQL语句将“旷工”人员的缺勤天数增加一天。 update JBQK set ts=ts+1 where lx='旷工'

10.使用SQL语句创建一个名为zgqq(职工缺勤)的视图,要求能够使用该视图查询缺勤2天以上的职工的职工号、姓名、缺勤天数和缺勤名称。 create view zgqq as

select zgh,xm,ts,mc from JBQK,QQLX

where JBQK.lx=QQLX.lx and ts>2

(二)某大学的运动会比赛项目管理数据库包括如下三张表: Student(xh,xm,xb,nl,szx)

各属性分别表示学生的(学号、姓名、性别、年龄、所在系)。 Sports(xmh,xmm,dw)

各属性分别表示(运动项目的编号、名称、项目的计分单位)。 SS(xh,xmh,cj)

各属性分别表示(学号、运动项目的编号、成绩)。 根据上述情况,完成如下操作:

1. 使用SQL语言,创建student表,并定义xh为主键。 create table student(

xh char(4) primary key, xm char(6), xb char(2), nl int, szx char(8))

2. 在Student表xh属性列上建立名称为xh的聚簇索引。 create cluster index Student_xh on student(xh);

3. 使用SQL语言 从表Student中删除学生“张三”的记录。 delete from student where xm=' 张三'

4. 使用SQL语言为SS表添加一条记录:学号为“xh001”的学生参与了编号为“xm001”的运动项目,但还没成绩。 insert into ss(xh,xmh) values('xh001','xm001') 5. 使用SQL语言,将Student表学号为“xh001”的学生的姓名改为“李明”。

update student set xm='李明' where xh='xh001' 6. 查询“计算机”系的学生参加了哪些运动项目,只把运动项目名称列出,去除重复记录。 select distinct xmm from student,sports,ss

where student.xh=ss.xh and sports.xmh=ss.xmh and szx='计算机'

7. 查询各个系的学生的“跳高”项目比赛的平均成绩 (不要求输出比赛项目的计分单位) 。 select avg(cj) from student,sports,ss

where student.xh=ss.xh and sports.xmh=ss.xmh and xmm='跳高' group by szx;

8. 统计各个系的总成绩情况,并根据总成绩按降序排序。 select count(cj) 总成绩 from student,ss where student.xh=ss.xh group by szx order by 总成绩

9. 建立“计算机”系所有男学生的信息视图JSJ_M_Student。 create view JSJ_M_Student as

select * from Student

where szx='计算机' and xb='男' 10.

回收用户“李明”对Sports表的查询权限。

revoke select on table Sports from 李明

(三)有一个“学生选课”数据库,数据库中包括三个表,其关系模式分别为: