实验5sql语句练习——图书馆数据库答案

实验5sql语句练习——图书馆数据库答案
实验5sql语句练习——图书馆数据库答案

实验5sql语句练习一一图书馆数据库

实验5 Sql语句练习一一图书馆数据库

实验目的

(1)了解SQL SerVer数据库的逻辑结构和物理结构;

(2)了解表的结构特点;

(3)了解SQL SerVer的基本数据类型;

(4)了解空值概念;

(5)学会在企业管理器中创建数据库和表;

(6)学会使用T-SQL语句创建数据库和表。

(7)学会使用T-SQL语句更新数据。

(7)学会使用T-SQL语句创建多种查询。

实验准备

首先要明确,能够创建数据库的用户必须是系统管理员,或是被授权使用CREATE DATABASE语句的用户。

其次创建数据库必须要确定数据库名、数据库大小(最初的大小、最大的大小、是否允许增长及增长方式)和存储数

据库的文件。

然后,确定数据库包含哪些表,以及所包含的各表的结构,还要了解SQL SerVer的常用数据类型,以创建

数据库的表。

此外还要了解两种常用的创建数据库、表的方法,即在企业管理器中创建和使用T-SQL的CREATE DATABASE 语句。

实验内容

假设有5本书

设有一图书馆数据库,其中包括3个表,即图书表、读者表和借阅表。三个表的结构如图: 图书表结构

读者表结构

假设有10位读者

借阅表结构

Create database Lab05

(2) 用Sql语句创建上述3个表

Create table book

(

bookId Char(Io)Primary key, bookName VarChar(50), bookWriter VarChar(30), bookPublish VarChar(30), bookPrice float

)

Create table reader

(

readerId Char(10) Primary key, readerName VarChar(8)not null, readerSex char(2)not null, readerOfficeTel char(8), readerDepartment VarChar(30)

)

Create table 借阅表

(

readerId Char(10), bookId Char(10), CheCkOUtTime char(8), CheCkInTime char(8), Primary

key(readerId,bookId), foreign key (readerId) references reader(readerId), foreign key (bookId) references book(bookId),

)

(3) 基于图书馆数据库的3个表,用sql语言完成一下操作:

1) 为图书表增加一列“ ISBN ”,数据类型为CHAR(Io)

alter table book add ISBN char(10)

2) 为刚添加的ISBN 列增加缺省值约束,约束名为ISBNDEF ,缺省值为‘ 7111085949' ALTER TABLE book

ADD CONSTRAINT ISBNDEF DEFAULT ('7111085949') FOR ISBN

3) 删除图书表中ISBN 列增加的缺省值约束alter table book drop ISBNDEF

4) 删除图书表中新增的ISBN 列ALTER TABLE book DROP COLUMN ISBN

5) 查询全体图书的图书号、书名、作者、出版社和单价select

bookId,bookName,bookWriter,bookPublish,bookPrice from book

6) 查询全体图书的信息,其中单价打8 折,并设置该列的别名为‘打折价'

select bookId,bookName,bookWriter,bookPublish,(bookPrice*0.8) as 打折价from book

7) 显示所有借阅者的读者号,并去掉重复行select distinct readerId from 借阅表

8) 查询所有单价在20—30 元之间的图书信息

select bookId,bookName,bookWriter,bookPublish,bookPrice,ISBN from book

where bookPrice between 20 and 30

9) 查询机械工业出版社、科学出版社、人民邮电出版社的图书信息

select bookId,bookName,bookWriter,bookPublish,bookPrice,ISBN from book

where bookPublish in(' 机械工业出版社' , '科学出版社',' 人民邮电出版社')

10 ) 查询既不是机械工业出版社、人民邮电出版社、也不是科学出版社出版的图书信息

select bookId,bookName,bookWriter,bookPublish,bookPrice,ISBN from book

where bookPublish not in(' 机械工业出版社' , ' 科学出版社','人民邮电出版社')

11) 查询姓名的第二个字符是 '建',并且只有2 个字的读者的读者号及姓名

select readerId,readerName from reader where readerName like '_ 建'

12 ) 查询姓名不是以‘王' 、‘张'或‘李'开头的所有读者的读者号及姓名

【方式一】查询出来的结果有问题!select readerId,readerName from reader where readerName not in (' 王%','张%','李%') 【方式二】select readerId,readerName from reader where readerName not in ( select readerName

from reader

Where readerName like'王%'or readerName like'张%'or readerName like'李%' )

13 ) 查询无归还日期的借阅信息

select book.bookId,book.bookName,reader.readerId,

reader.readerName, 借阅表.checkOutTime as 借书时间, 借阅表.checkInTime as 还书时间from 借阅表,book,reader

where 借阅表.bookId = book.bookId

and 借阅表.readerId = reader.readerId

and 借阅表.checkInTime is null

14 ) 查询机械工业出版社图书的平均价格、最高价、最低价

select avg(bookPrice)as 平均价格,max(bookPrice) as 最高价,min(bookPrice) as 最低价from book where bookPublish = ' 机械工业出版社图书'

15 ) 查询读者的基本信息及借阅情况

select reader.readerId,reader.readerName , 借阅表.bookId,book.bookName,book.bookPublish from reader , 借阅表,book

where reader.readerId = 借阅表.readerId and book.bookId = 借阅表.bookId and 借阅表.readerId ='1000000007'

16) 查询至少借阅过1 本机械工业出版社出版的图书的读者的读者号、姓名、书名及借阅本数,并按借

阅本书多少降序排列

select r.Rno,Rname,count(borrow.Bno) 借阅册数

from borrow,b,r

where b.bno=borrow.bno and press=' 机械工业出版社' and borrow.rno=r.rno group by r.rno,Rname

order by count(borrow.Bno) desc

17 ) 查询与‘王小平'的办公电话相同的读者的姓名

/* 使用“自连接方式”求解*/

select b.readerName,b.readerId,a.readerOfficeTel

from reader a, reader b

where a.readerName like ' 王小平'and a.readerOfficeTel = b.readerOfficeTel

/*

18 ) 查询所有单价小于平均单价的图书的书号、书名及出版社

select bookId,bookName,bookPublish,bookPrice from book where bookPrice <

(

select avg(bookPrice) as averagePrice

from book

)

19 ) 查询‘科学出版社'的图书单价比‘机械工业出版社'最高单价还高的图书书名及单价

select bookId,bookName,bookPublish,bookPrice from book

where bookPublish like '科学出版社' and bookPrice >

(

select max(bookPrice)

from book

where bookPublish = ' 机械工业出版社'

)

20) 查询‘科学出版社'的图书中单价比‘机械工业出版社'最低单价高的图书书名及单价

select bookId,bookName,bookPublish,bookPrice

from book

where bookPrice <

(

select max(bookPrice)

from book

where bookPublish = ' 机械工业出版社'

)

and bookPublish like ' 科学出版社'

21 ) 创建机械工业出版社图书的视图

CREATE VIEW 机械工业出版社View

AS

SELECT bookId,bookName,bookPrice

FROM book

WHERE bookPublish = ' 机械工业出版社'

22 ) 创建一个借阅统计视图,名为CountView ,包含读者的读者号和总借阅本数

CREATE VIEW CountView

AS

SELECT readerId as 读者号,count(*) as 总借阅本数

FROM 借阅表

GROUP BY readerId

23 ) 创建一个借阅统计视图,名为CountView10, 包含借阅总本数打于2 的读者号和总借阅本数

CREATE VIEW 借阅统计视图View

AS

SELECT readerId as 读者号,count(*) as 总借阅本数

FROM 借阅表

GROUP BY readerId

HA VING COUNT(*) >2

相关主题
相关文档
最新文档