数据库系统原理及应用教程(第3版)课后习题答案!

数据库系统原理及应用教程(第3版)课后习题答案!,第1张

习题5第5题p148

create database 职工_社团

use 职工_社团

create table 职工(

职工号 char(10) primary key,

姓名 char(8),

年龄 smallint default 20,

性别 char(20),

constraint C1 check (性别 in ('男','女')));

create table 社会团体(

编号 char(10) primary key,

名称 char(8),

负责人 char(10),

活动地点 char(20),

constraint C2 foreign key (负责人) references 职工 (职工号));

create table 参加(

职工号 char(10),

编号 char(10),

参加日期 smalldatetime,

constraint C3 primary key (职工号,编号),

constraint C4 foreign key (职工号) references 职工 (职工号),

constraint C5 foreign key (编号) references 社会团体 (编号));

(2)

create view 社团负责人(编号,名称,负责人职工号,负责人姓名,负责人性别)

as select 社会团体编号,社会团体名称,社会团体负责人, 职工职工号,职工性别

from 职工,社会团体,参加

where 社会团体编号=参加编号 and 职工职工号=参加职工号

create view 参加人情况(职工号,姓名,社团编号,社团名称,参加日期)

as select 参加职工号,姓名,社会团体编号,名称,参加日期

from 职工,社会团体,参加

where 职工职工号=参加职工号 and 参加编号=社会团体编号

(3)

select distinct 职工职工号,姓名

from 职工,社会团体,参加

where 职工职工号=参加职工号 and 参加编号=社会团体编号

and 社会团体名称 in('歌唱队','篮球队');

(4)

select

from 职工

where not exists (select

from 参加

where 参加职工号=职工职工号);

(5)

select from 职工

where not exists

(select

from 社会团体

where not exists

(select

from 参加

where 参加职工号=职工职工号 and 参加编号=社会团体编号));

(6)

select 职工号

from 职工

where not exists (select

from 参加 参加1

where 参加1职工号='001'and not exists

(select

from 参加 参加2

where 参加2编号=参加1编号 and 参加2职工号=职工职工号))

(7)

select 编号,count(职工号) as 参加人数

from 参加

group by 编号;

(8)

select TOP 1 名称,count() 参加人数

from 参加,社会团体

where 参加编号=社会团体编号

group by 名称

order by 参加人数 desc

(9)

select distinct 社会团体名称,职工姓名 as 负责人

from 职工,社会团体,参加

where 社会团体编号=参加编号

and 社会团体负责人=职工职工号

and 参加编号 in(select 参加编号

from 参加

group by 参加编号 having count(参加编号)>100)

(10)

grant select,insert,delete on 社会团体 to 李平

with grant option;

grant select,insert,delete on 参加 to 李平

with grant option;

习题6第9题p212

create database 学生选课

use 学生选课

create table 学生(

学号 char(10) primary key,

姓名 char(10),

性别 char(10),

constraint C1 check (性别 in ('男','女')),

年龄 smallint default 20,

所在系 char(20));

create table 课程(

课程号 char(10) primary key,

课程名 char(20),

先行课 char(20));

create table 选课(

学号 char(10),

课程号 char(10),

成绩 smallint,

constraint D1 primary key (学号,课程号),

constraint D2 foreign key (学号) references 学生(学号),

constraint D3 foreign key (课程号) references 课程(课程号))

create index student_ind on 学生(学号)

create index class_ind on 课程(课程号)

create index select_ind on 选课(学号,课程号)

create rule value_rule as @value in ('男','女')

go

exec sp_bindrule 'value_rule','学生性别'

go

create default 性别缺省 as '男'

go

exec sp_bindefault '性别缺省','学生性别'

go

create trigger 选课插入更新 on 选课

for insert,update

as if (select count()

from 学生,inserted,课程

where 学生学号=inserted学号 and 课程课程号=inserted课程号)=0

rollback transaction

go

create trigger delete_all on 学生

for delete

as delete 选课

from 选课,deleted

where 选课学号=deleted学号

go

select 所在系,count(学号)as 学生人数

from 学生

group by 所在系

order by 所在系

compute count(所在系),sum(count(学号))

select

from 学生 inner join 选课 on 学生学号=选课学号

go

select

from 学生 left outer join 选课 on 学生学号=选课学号

go

select

from 学生 right outer join 选课 on 学生学号=选课学号

go

select 选课学号,学生姓名,

学习情况=case

when avg(成绩)>=85 then '好'

when avg(成绩)>=75 and avg(成绩)<85 then '较好'

when avg(成绩)>=60 and avg(成绩)<75 then '一般'

when avg(成绩)<60 then '较差'

end

from 学生,选课

where 学生学号=选课学号

group by 选课学号,姓名

go

只有这些,不知道用得到吗

邮件已经发出,过几分钟后记得查收(可能在垃圾箱里边),收到后觉得还算满意请点下边的采纳通知我。如果10分钟后还没收到,请直接在本问题里追问我,我会再次发送。

最后如果在采纳之余能加点分数!

二、填空题

1数据库文件由数据文件和( 日志 )文件构成。

2完整性约束主要包括( 实体 )完整性、域完整性和( 参照 )完整性。

3数据库系统的应用结构C/S结构的中文全称是( 客户服务器结构 ),B/S结构的中文全称是( 浏览器/服务器结构 )。

4 SQL server局部变量名字必须以( @ )开头。

5创建、修改和删除数据库对象的语句分别是create、(alter )和(drop )。

6在SQL server中计算最大、最小、平均、求和与计数的聚合函数是max、min、(avg )、( sum )和( count )。

7数据完整性用于保证数据库中数据的( 正确性 )、( 一致性 )和(可靠性 )。

8触发器在工作过程中会产生两张临时的表,即( inserted )和( deleted )。

9create procedure是用来创建( 存储过程 )的语句。

10根据题意补充代码:

Student表如表1所示,sc表如表3所示。

(1) 查询学生总人数

代码:select count() as 总人数from student

(2) 计算C01课程的平均成绩

代码:Select avg(成绩) as平均成绩from sc where 课程号= ‘C01 ‘

(3) 求计算机系的学生学号和姓名;

代码:select 学号,姓名from student

Where 系别=’计算机系’

(4)创建触发器,限制不能删除有人选的课程。

代码:

CREATE trigger limit ON 课程

FOR delete

AS

IF EXISTS (SELECT 课程课程号 FROM 课程 JOIN 选课 ON 课程课程号 = 选课课程号)

Print ‘该课程有人选,不能删除’

rollback

评卷人 得分

三、简答题

1 简述什么是实体完整性。

实体完整性是用于保证数据表的每一个特定实体记录都是唯一的。

2简述视图与基本表的区别和联系。

区别:1、视图是已经编译好的sql语句。而表不是

2、视图没有实际的物理记录。而表有。

3、表是内容,视图是窗口

4、视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL语句的集合。从安全的角度说,视图可以不给用户接触数据表,从而不知道表其他内容。表是实表;视图是虚表。

6、视图的建立和删除只影响视图本身,不影响对应的基本表。

联系:视图是在基本表之上建立的表,它的结构和内容都来自基本表,它依据基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本表的抽象和在逻辑意义上建立的新关系

四、编程题

1表1:student表(学生表)

学号 姓名 性别 年龄 系别

1 周杨 男 18 计算机系

2 沈晔 女 21 经管系

3 张宁萍 女 19 电子工程系

4 李立初 男 19 艺术系

表2:course表(课程表)

课程号 课程名 学分

C01 SQLServer 4

C02 数据结构 3

C03 专业英语 2

表3:sc表(选课表)

学号 课程号 成绩

1 C01 88

2 C01 90

2 C02 70

3 C03 79

1、 把course表中课程号为3的课程的学分修改为3。

Update course

Set 学分=’3’

Where 课程=’3’

Select from course

2、 在student表中查询年龄大于18的学生的所有信息,并按学号降序排列。

Select from student

Where 年龄>18

Order by 学号 desc

3、 在以上三个表中查询选的课程的“学分”为3,并且成绩大于80的学生的学号、姓名

和性别。

Select student学号, student姓名, student性别fromstudent, course, sc

Where student学号= sc学号 and course课程号= sc课程号 and course学分=’3’ and sc成绩>’80’

4、 将student表的列系别改为nchar(8)数据类型,并且不允许为空。

Alter table student

Alter column 系别 nchar(8) not null

5、 在student表中插入彭夏雨同学的记录。姓名:彭夏雨,学号:4,年龄:20

Insert student

Values(‘彭夏雨’,’4’,’ 20’)

6声明一个长度为8的字符型变量“shuaige”, 并赋初值为”陈俊杰”。请按前面的要求写

出相应的语句。

Declare@shuaige char(8)

Set@shuaige=’陈俊杰’

7、查询周杨同学所上课程的成绩。

Selectsc成绩from student,sc

Wherestudent学号=sc学号 and姓名=’周扬’

1

SELECT cname,avg(grade) 平均成绩

FROM Course join sc on coursecno=sccno

where teacher like '张%'

2

SELECT CNO,COUNT() 人数

FROM SC

GROUP BY CNO

ORDER BY 2 DESC,1

3

SELECT sname,sno

FROM student

where sno in(select sno

from sc

where cno in(select cno

from student join sc on studentsno=scsno

where sname='张三'))

and sname<>‘张三’

4

UPDATE SC SET GRADE=GRADE+5

FROM SC JOIN student on scsno=studentsno

join course on coursecno=sccno

where ssex='女' and cname='计算机基础'

5

DELETE SC

FROM SC JOIN student on scsno=studentsno

where sname='王五'

6

select cname

from student join sc on studentsno=scsno

join course on coursecno=sccno

where sname in('张三','李四')

group by cname

having count()=2

7

select sname

from student

where sno not in(select sno from sc)

以上就是关于数据库系统原理及应用教程(第3版)课后习题答案!全部的内容,包括:数据库系统原理及应用教程(第3版)课后习题答案!、求SQL Server数据库教程课后答案。赵明渊主编的。清华大学出版社。 课程封面如图:、数据库求答案等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9432045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存