文档库 最新最全的文档下载
当前位置:文档库 › 数据库习题答案

数据库习题答案

1. 写出建立BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束

--实现代码:

CREATE TABLE BORROW(

CNO int FOREIGN KEY REFERENCES CARD(CNO),

BNO int FOREIGN KEY REFERENCES BOOKS(BNO),

RDATE datetime,

PRIMARY KEY(CNO,BNO))



2. 找出借书超过5本的读者,输出借书卡号及所借图书册数

--实现代码:

SELECT CNO,借图书册数=COUNT(*)

FROM BORROW

GROUP BY CNO

HAVING COUNT(*)>5



3. 查询借阅了"水浒"一书的读者,输出姓名及班级

--实现代码:

SELECT * FROM CARD c

WHERE EXISTS(

SELECT * FROM BORROW a,BOOKS b

WHERE a.BNO=b.BNO

AND b.BNAME=N'水浒'

AND https://www.wendangku.net/doc/f12539659.html,O=https://www.wendangku.net/doc/f12539659.html,O)



4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期

--实现代码:

SELECT * FROM BORROW

WHERE RDATE


5. 查询书名包括"网络"关键词的图书,输出书号、书名、作者

--实现代码:

SELECT BNO,BNAME,AUTHOR FROM BOOKS

WHERE BNAME LIKE N'%网络%'



6. 查询现有图书中价格最高的图书,输出书名及作者

--实现代码:

SELECT BNO,BNAME,AUTHOR FROM BOOKS

WHERE PRICE=(

SELECT MAX(PRICE) FROM BOOKS)



7. 查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出

--实现代码:

SELECT https://www.wendangku.net/doc/f12539659.html,O

FROM BORROW a,BOOKS b

WHERE a.BNO=b.BNO AND b.BNAME=N'计算方法'

AND NOT EXISTS(

SELECT * FROM BORROW aa,BOOKS bb

WHERE aa.BNO=bb.BNO

AND bb.BNAME=N'计算方法习题集'

AND https://www.wendangku.net/doc/f12539659.html,O=https://www.wendangku.net/doc/f12539659.html,O)

ORDER BY https://www.wendangku.net/doc/f12539659.html,O DESC



8. 将"C01"班同学所借图书的还期都延长一周

--实现代码:

UPDATE b SET RDATE=DATEADD(Day,7,b.RDATE)

FROM CARD a,BORROW b

WHERE https://www.wendangku.net/doc/f12539659.html,O=https://www.wendangku.net/doc/f12539659.html,O

AND a.CLASS=N'C01'



9. 从BOOKS表中删除当前无人借阅的图书记录

--实现代码:

DELETE A FROM BOOKS a

WHERE NOT EXISTS(

SELECT * FROM BORROW

WHERE BNO=a.BNO)



10. 如果经常按书名查询图书信息,请建立合适的索引

--实现代码:

CREATE CLUSTERED INDEX IDX_BOOKS_BNAME ON BOOKS(BNAME)



11. 在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",就将该读者的借阅记录保存在BORROW_SAVE表中(注ORROW_SAVE表结构同BORROW表)

--实现代码:

CREATE TRIGGER TR_SAVE ON BORROW

FOR INSERT,UPDATE

AS

IF @@ROWCOUNT>0

INSERT BORROW_SAVE SELECT i.*

FROM INSERTED i,BOOKS b

WHERE i.BNO=b.BNO

AND b.BNAME=N'数据库技术及应用'



12. 建立一个视图,显示"力01"班学生的借书信息(只要求显示姓名和书名)

--实现代码:

CREATE VIEW V_VIEW

AS

SELECT https://www.wendangku.net/doc/f12539659.html,,b.BNAME

FROM BORROW ab,CARD a,BOOKS b

WHERE https://www.wendangku.net/doc/f12539659.html,O=https://www.wendangku.net/doc/f12539659.html,O

A

ND ab.BNO=b.BNO

AND a.CLASS=N'力01'



13. 查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出

--实现代码:

SELECT https://www.wendangku.net/doc/f12539659.html,O

FROM BORROW a,BOOKS b

WHERE a.BNO=b.BNO

AND b.BNAME IN(N'计算方法',N'组合数学')

GROUP BY https://www.wendangku.net/doc/f12539659.html,O

HAVING COUNT(*)=2

ORDER BY https://www.wendangku.net/doc/f12539659.html,O DESC



14. 假定在建BOOKS表时没有定义主码,写出为BOOKS表追加定义主码的语句

--实现代码:

ALTER TABLE BOOKS ADD PRIMARY KEY(BNO)



15.1 将NAME最大列宽增加到10个字符(假定原为6个字符)

--实现代码:

ALTER TABLE CARD ALTER COLUMN NAME varchar(10)



15.2 为该表增加1列NAME(系名),可变长,最大20个字符

--实现代码:

ALTER TABLE CARD ADD 系名 varchar(20)

本文来自CSDN博客,转载请标明出处:https://www.wendangku.net/doc/f12539659.html,/gragel/archive/2010/05/01/5549220.aspx

create database mydb//建数据库
go
use mydb
////////////////建student表
create table student
(
--学号
sno varchar(3) not null primary key,// 主键
--姓名
sname varchar(4) not null,
--性别
ssex varchar(2) not null,
--出生年月
sbirthday datetime,
--所在班级
class varchar(5)
)
////////////创建teacher表
create table teacher
(
--教工编号
tno varchar(3) not null primary key,// 主键
--教工姓名
tname varchar(4) not null,
--教工性别
tsex varchar(2) not null,
--教工出生日期
tbirthday datetime,
--职称
prof varchar(6),
--所在部门
depart varchar(10)
)
//////////////创建课程表
create table course
(
--课程号
cno varchar(5) not null primary key ,// 主键
--课程名称
cname varchar(10) not null,
--教工编号
tno varchar(3) references teacher(tno) // 外键
)
/////////创建分数表
create table score
(
sid int identity(1,1) not null primary key,// 主键,自增长列
--学号
sno varchar(3) not null references student(sno),// 外键
--课程号
cno varchar(5) not null references course(cno),// 外键
--成绩
degree decimal(4,1)
)
//插入数据
insert into student
values('108','曾华','男','1977-09-01','95033')
insert into student
values('105','匡明','男','1975-10-02','95031')
insert into student
values('107','王丽','女','1976-01-23','95033')
insert into student
values('101','李军','男','1976-02-20','95033')
insert into student
values('109','王芳','女','1975-02-10','95031')
insert into student
values('103','陆君','男','1974-06-03','95031')
insert into teacher
values('804','李诚','男','1958-12-02','副教授','计算机系')
insert into teacher
values('856','张旭','男','1969-03-12','讲师','电子工程系')
insert into teacher
values('825','王萍','女','1972-05-05','助教','计算机系')
insert into teacher
values('831','刘冰','女','1958-08-14','助教','电子工程系')
insert into course
values('3-105','计算机导论','825')
insert into co

urse
values('3-245','操作系统','804')
insert into course
values('6-166','数字电路','856')
insert into course
values('9-888','高等数学','831')
insert into score
values('103','3-245','86')
insert into score
values('105','3-245','75')
insert into score
values('109','3-245','68')
insert into score
values('103','3-105','92')
insert into score
values('105','3-105','88')
insert into score
values('109','3-105','76')
insert into score
values('101','3-105','64')
insert into score
values('107','3-105','91')
insert into score
values('108','3-105','78')
insert into score
values('101','6-166','85')
insert into score
values('107','6-166','79')
insert into score
values('108','6-166','81')
select * from student
select * from teacher
select * from course
select * from score
--查询Student表中的所有记录的Sname、Ssex和Class列--
SELECT Sname,Ssex,Class FROM Student
--查询教师所有的单位 即不重复的Depart列--
select Depart from teacher group by Depart
select distinct Depart from teacher
--查询Student表的所有记录--
select * from Student
--查询Score表中成绩在60到80之间的所有记录--
select degree from Score where degree>=60 and degree<=80
select degree from Score where degree between 60 and 80
--查询Score表中成绩为85,86或88的记录--
select degree from Score where degree=85 or degree=86 or degree=88
select degree from Score where degree in (85,86,88)
--查询Student表中“95031”班或性别为“女”的同学记录--
select * from Student where class='95031' or ssex='女'
--以Class降序查询Student表的所有记录--
select * from Student order by Class desc
-- 以 Cno 升序、 Degree 降序查询 Score 表的所有记录 --
select * from Score order by Cno, Degree desc
--查询“95031”班的学生人数--
select count(*) from Student where class='95031'
-- 查询 Score 表中的最高分的学生学号和课程号 --
select top 1 sno,cno from Score order by degree desc
--11、查询‘3-105’号课程的平均分--
select avg(degree) from Score where cno='3-105'
--12 、查询 Score 表中至少有 5 名学生选修的并以 3 开头的课程的平均分数
select avg(degree)from Score where
(select count(sno) from student where sno
in(select sno from score where cno
in(select cno from course where cnolike '3%' group by cno)))>=5
select avg(degree) from score where
--13、查询最低分大于70,最高分小于90的Sno列
select Sno from Score where degree>70 and degree<90
--14 、查询所有学生的 Sname 、 Cno 和 Degree 列 // 多表连接查询
select Sname,Cno,Degree from Score inner join Student on Score.sno=Student.sno
--15、查询所有学生的Sno、Cname和Degree列
select Sno,Cname,Degree from course inner join Score on https://www.wendangku.net/doc/f12539659.html,o=https://www.wendangku.net/doc/f12539659.html,o
--16 、查询所有学生的 Sname 、 Cname 和 Degree 列 // 三表联查
SELECT dbo.score.Degree,https://www.wendangku.net/doc/f12539659.html,ame, dbo.student.Sname
FROM dbo.course inner

join
dbo.score on https://www.wendangku.net/doc/f12539659.html,o = https://www.wendangku.net/doc/f12539659.html,o inner join
dbo.student on dbo.score.sno = dbo.student.sno
--17、查询“95033”班所选课程的平均分
select avg(degree)from Score where sno in
(select sno from student where class='95033')
--18、假设使用如下命令建立了一个grade表:
--create table grade(low int,upp int,rank varchar(1))
--insert into grade values(90,100,'A')
--insert into grade values(80,89,'B')
--insert into grade values(70,79,'C')
--insert into grade values(60,69,'D')
--insert into grade values(0,59,'E')
--现查询所有同学的Sno、Cno和rank列
create table grade(low int,upp int,rank varchar(1))
insert into grade values(90,100,'A')
insert into grade values(80,89,'B')
insert into grade values(70,79,'C')
insert into grade values(60,69,'D')
insert into grade values(0,59,'E')
select* from grade
SELECT dbo.grade.rank, dbo.score.sno, https://www.wendangku.net/doc/f12539659.html,o
FROM dbo.grade CROSS JOIN dbo.score
--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。//无关子查询
declare @snodegree int //定义变量
select @snodegree=degree from Score where sno='109'and cno='3-105'
select * from Score where degree > @snodegree and cno='3-105'
--20、查询score中选学多门课程的同学中分数为非最高分成绩的记录
select * from score
where degree <(select top 1 degree from score order by degree desc )
and sno in (select sno from score group by sno having count (sno)>1)
--21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
select * from score
where degree >(select degree from score where sno='109' and cno='3-105')
--22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列
select Sno,Sname,Sbirthday from Student where year ( Sbirthday) in (select year(Sbirthday) from student where sno=108)
--23 、查询“张旭“教师任课的学生成绩
select degree from score where cno in (select cno from course where tno =(select tno from teacher where tname=' 张旭 ' ))
--24、查询选修某课程的同学人数多于5人的教师姓名// 三表查询
Select th.tname from teacher th, course cou ,score sco
where th.tno=cou.tno and https://www.wendangku.net/doc/f12539659.html,o=https://www.wendangku.net/doc/f12539659.html,o group by th.tname, https://www.wendangku.net/doc/f12539659.html,o having count(*)>5
--25、查询95033班和95031班全体学生的记录
select * from student where class='95033' or class='95031'
--26、查询存在有85分以上成绩的课程Cno
select Cno from score where degree>=85
--27、查询出“计算机系“教师所教课程的成绩表
select * from score where cno in(select cno from course where tno in (select tno from teacher where depart=' 计算机系 ' ))
--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof
select tname,prof from teacher where depart='计算机系' or depart='电子工程系'
--29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cn

o、Sno和Degree, --并按Degree从高到低次序排序
select cno,sno,degree
from score
where degree in(select degree from score where cno='3-105'and degree>all(select degree from score where cno='3-245'))
order by degree desc
--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree
select cno,sno,degree
from score
where degree in(select degree from score where cno='3-105'and degree>all(select degree from score where cno='3-245'))
--31、查询所有教师和同学的name、sex和birthday
SELECT student.sname,student.sbirthday,student.ssex,teacher.tname,teacher.tsex,teacher.tbirthday
FROM score INNER JOIN student
ON score.sno = student.sno
CROSS JOIN teacher
--32、查询所有“女”教师和“女”同学的name、sex和birthday
SELECT student.sname,student.sbirthday,student.ssex,teacher.tname,teacher.tsex,teacher.tbirthday
FROM teacher
inner join course
on teacher.tno=course.tno
inner join score
on https://www.wendangku.net/doc/f12539659.html,o=https://www.wendangku.net/doc/f12539659.html,o
inner join student
on score.sno=student.sno
where ssex='女' and tsex='女'
--33、查询成绩比该课程平均成绩低的同学的成绩表
select * from score where degree --34、查询所有任课教师的Tname和Depart
select tname,depart from teacher where tno in(select tno from course)
--35 查询所有未讲课的教师的Tname和Depart
select tname,depart from teacher where tno not in (select tno from course)
--36、查询至少有2名男生的班号
select class from student where (select count(sno)from student where ssex='男')>=2
--37、查询Student表中不姓“王”的同学记录
select * from student where sname not like ' 王 %'
--38、 查询 Student表中每个学生的姓名和 年龄
select sname,
sage=( select datediff (yy,sbirthday,getdate()))from student
--39 、查询 Student 表中最大和最小的 Sbirthday 日期值
select Sbirthday
from student
where Sbirthday>=all(select Sbirthday from student)
or Sbirthday<=all(select Sbirthday from student)
--40、以班号和年龄从大到小的顺序查询Student表中的全部记录
select sno,sname,ssex,sbirthday,class,sage=(select datediff(yy,sbirthday,getdate()))
from student
order by class,sage desc
--41、查询“男”教师及其所上的课程
SELECT https://www.wendangku.net/doc/f12539659.html,ame,teacher.tname
FROM course INNER JOIN
score ON https://www.wendangku.net/doc/f12539659.html,o = https://www.wendangku.net/doc/f12539659.html,o INNER JOIN
student ON score.sno = student.sno INNERJOIN
teacher ON course.tno = teacher.tno
where tsex='男' group by cname,tname
--42、查询最高分同学的Sno、Cno和Degree列
select top 1 sno,cno,degree from score order by degree desc
--43、查询和“李军”同性别的所有同学的Sname
select sname from student where ssex =(select ssex from student where sname='李军')
--44、 查询和“李军”同性别并同班的同学 Sname
select sname from student where ssex =(select ssex from student where sname='李军') and class=(selec

t class from student where sname='李军')
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select * from score where sno in(select sno from student where ssex='男') and cno in(select cno from course where cname='计算机导论')
--46、查询score表中分数最高的学生的信息。//多层嵌套
select * from student where sno in(select sno from score where degree =(select top1 degree from score order by degree desc))
--47、查询score表中的平均分在80分以上的学生信息
select * from student where sno in(select sno from score where degree >(select avg(degree) from score group by cno))
--创建以下三个表:学生表: Student(Sno,Sname,Ssex,Sbirthday,Sage,Sdept) Student 由学号 (Sno) 、姓名 (Sname)、性别 (Ssex)、出生日期 (Sbirthday)、年龄 (Sage),所在系 (Sdept) 五个属性组成, 其中 Sno 为主键.
-- 课程表: Coures(Cno,Cname,Cpno,Ccredit)Coures 由课程号 (Cno)、课程名 (Cname)、选修课号 (Cpno)、学分 (Ccredit) 四个属性组成,其中 Cno 为主键.
-- 学生选课表: SC(Sno,Cno,Grade)SC 由学号 (Sno)、 课程号 (Cno)、 成绩 (Grade) 三个属性组成, 主键为 (Sno,Cno).
--按照以上三个表来做如下的习题:
create table studentInfo
(
Sno int not null primary key,
Sname varchar(100),
Ssex varchar(2),
Sbirthday datetime,
Sage int,
Sdept varchar(100)
)
go
create table courseInfo
(
Cno varchar(10) not null primary key,
Cname varchar(10),
Cpno int,
Ccredit int
)
go
create table SC
(
Sno int not null references studentInfo(sno),
Cno varchar(10) not null references courseInfo(cno),
Grade int
)
go
insert into studentInfo
values(001,'匡明','男','1975-10-02',34,'计算机系COMPUTER')
insert into studentInfo
values(002,'王丽','女','1976-01-23',23,'电子工程系DIANZIGONGCHENG')
insert into studentInfo
values(003,'李军','男','1976-02-20',43,'经济管理系JINGJIGUANLI')
insert into studentInfo
values(004,'王芳','女','1975-02-10',44,'电子工程系DIANZIGONGCHENG')
insert into studentInfo
values(005,'陆君','男','1974-06-03',67,'计算机系COMPUTER')
insert into studentInfo
values(006,'欧阳风a','男','1976-06-03',67,'计算机系COMPUTER')
insert into studentInfo
values(007,'欧阳风','男','1974-06-03',67,'计算机系COMPUTER')
insert into courseInfo
values('c1','计算机导论',825,120)
insert into courseInfo
values('f2','操作系统',804,100)
insert into courseInfo
values('b3','数字电路',856,150)
insert into courseInfo
values('z4','高等数学',831,200)
insert into SC values(002,'z4',56)
insert into SC values(002,'c1',89)
insert into SC values(002,'f2',56)
insert into SC values(002,'b3',89)
insert into SC values(003,'f2',46)
insert into SC values(003,'c1',100)
insert into SC values(003,'c1',89)
insert into SC values(003,'c1',98)
insert into SC values(004,'c1',83)
insert into SC values(004,'b3',82)
insert into SC values(004,'f2',23)
insert into

SC values(004,'b3',43)
insert into SC values(005,'c1',45)
insert into SC values(005,'b3',56)
insert into SC values(005,'f2',67)
insert into SC values(005,'b3',85)
--1.查找全体学生的学号与姓名
select sno,sname from studentInfo
--2.查找全体学生的姓名、学号与所在系
select sname,sno,sdept from studentInfo
--3.查找全体学生的详细纪录
select * from studentINFO
--4.查找全体学生的姓名和其出生日期
select sname,sbirthday from studentInfo
--5.查找全体学生的姓名、出生日期和所有系,要求用小写字母表示所有系名
select sname,sbirthday,LOWER(sdept) from studentInfo
--6.查找全体学生的姓名、出生日期和所有系,要求给这几列起别名
select sname 学生的姓名,sbirthday 出生日期,sdept 所有系 from studentInfo
--7.查找选修了课程的学生学号(不能重复)
select sno from SC group by sno
--8.查找计算系全体学生的名单
select sname from studentInfo
where sno in(select sno from SC where cno in(select cno from courseInfo where cname='计算机 导论'))
--9.查找所有年龄在20岁以下的学生姓名和其年龄(2种做法)
select sname,sage from studentInfo where sage<20
--10.查找考试成绩有不及格的学生的学号(不能重复)
select sno from SC where grade<30 groupby sno
--11.查找年龄在20--30岁(包括20岁和23岁)之间的学生的姓名、系别和年龄
select sname,sdept,sage from studentInfo where sage>=20 and sage<=30
--12.查找年龄不在20--30岁(包括20岁和23岁)之间的学生的姓名、系别和年龄
select sname,sdept,sage from studentInfo where sage<20 or sage>30
--13.查找经济管理系JINGJIGUANLI和计算机系COMPUTER学生的姓名与性别(用in做)
select sno,ssex from studentInfo where sdept in('经济管理系JINGJIGUANLI','计算机系COMPUTER')
--14.查找不在经济管理系JINGJIGUANLI和计算机系COMPUTER学生的姓名与性别(用not in做)
select sno,ssex from studentInfo where sdept not in('经济管理系JINGJIGUANLI','计算机系 COMPUTER')
--15.查找学号以为95001开头的学生的详细情况(用2种方法做)
select * from student where sno
--16.查找所有以“刘”开头的学生的姓名、学号和性别
select sno,sname,ssex from studentInfo where sname like '刘%'
--17.查找以“欧阳”开头的且三个汉字的学生的姓名
select sname from studentInfo where sname like '欧阳'
--18.查找名字中第2个字为“阳”字的学生的姓名和学号
select sname,sno from studentInfo wheresname like '_阳%'
--19.查找所有不以“刘”开头的学生姓名
select sname from studentInfo where sname not like '刘%'
--20.查找以“C”开头的课程的课程号和学分
select cno,Ccredit from courseInfo where cno like 'c%'
--21.查找以“H”开头,且倒数第三个字符为“T”的课程的课程号和学分
select cno,Ccredit from courseInfo where cno like

'H%T__'
--22.某些学生选修课程后没有参加考试,所以有选课纪录,但没有考试成绩。查找缺少成绩的学生的学 号和相应的课程号
select SC.sno,https://www.wendangku.net/doc/f12539659.html,o from studentInfo
inner join SC on studentInfo.sno =SC.sno
inner join courseInfo on https://www.wendangku.net/doc/f12539659.html,o=https://www.wendangku.net/doc/f12539659.html,o
where studentInfo.sno not in
(select sno from SC)
and https://www.wendangku.net/doc/f12539659.html,o in
(select cno from courseInfo)
--23.查找除了无成绩的学生的学号和课程号
select studentInfo.sno,https://www.wendangku.net/doc/f12539659.html,o from studentInfo
inner join SC on studentInfo.sno =SC.sno
inner join courseInfo on https://www.wendangku.net/doc/f12539659.html,o=https://www.wendangku.net/doc/f12539659.html,o
where studentInfo.sno not in(select sno from SC)
AND https://www.wendangku.net/doc/f12539659.html,o not in(select cno fromSC)
--24.查找计算机系COMPUTER年龄在20岁以下的学生姓名
SELECT sname from studentInfo where sdept='计算机系COMPUTER'
--25.查找是计算机系 或者是 数学系或者是 信息系的学生姓名和性别
SELECT sname,ssex from studentInfo where sdept='计算机系COMPUTER' or sdept='电子工程系 DIANZIGONGCHENG'
--26.查找选修了3号课程的学生的学号与其成绩,其查找结果按分数的降序排列
select sno,grade from SC where cno='b3' order by grade desc
--27.查找全体学生情况,查找结果按所在系的系号升序排列,同一系中的学生按年龄降序排列
select * from studentInfo order by sdept asc
select * from studentInfo where sdept in(select sdept from studentInfo ) order by sage desc
--28.查找学生总人数
select count(*) from studentInfo
--29.查找选修了课程的学生人数
--30.计算2号课程的学生平均成绩
select avg(grade)from sc where sno='002'
分享 [图] [图] [图] [图] [图]

阅读 ┊ 评论 ┊ 收藏 ┊ 转载 ┊ 顶 ▼ ┊ 打印 ┊ 举报
已投稿到: 排行榜 圈子
前一篇: 带有自定义标签风讯index
后一篇: [权限管理系统]用户管理测试用例

相关文档