上机实验题1
创建数据库
create database Library
on (
name=a,
filename='D:\DB\library.mdf',
size=10MB,
maxsize=10MB,
filegrowth=10MB
)
Log on(
name=la,
filename='D:\DB\library.ldf',
size=10MB,
maxsize=10MB,
filegrowth=10MB
)
创建四张表
create table depart(
班号 char(10) primary key,
系名 char(10)
)
create table student(
学号 int primary key,
姓名 char(20),
性别 char(10),
出生日期 datetime,
班号 char(10) constraint class_num foreign key references depart(班号)
)
create table book(
图书编号 int primary key,
图书名 char(10),
作者 char(10),
定价 int ,
出版社 char(100)
)
create table borrow(
学号 int constraint fk_study_num foreign key references student(学号),
图书编号 int constraint fk_lb_num foreign key references book(图书编号),
primary key(学号,图书编号),
借书日期 datetime
)
插入四张表的数据
insert into depart values('0501','计算机系');
insert into depart values('0502','计算机系');
insert into depart values('0801','电子工程系');
insert into depart values('0802','电子工程系');
insert into student values(1,'张任','男','1995-01-02','0501');
insert into student values(2,'程华','男','1996-01-10','0501');
insert into student values(3,'张丽','女','1995-06-07','0502');
insert into student values(4,'王英','女','1994-12-10','0502');
insert into student values(5,'李静','男','1995-04-05','0502');
insert into student values(10,'许兵','男','1995-08-10','0801');
insert into student values(11,'张功','男','1995-06-02','0801');
insert into student values(12,'李华','男','1994-10-03','0801');
insert into student values(13,'马超','男','1996-02-03','0802');
insert into student values(14,'曾英','女','1994-03-06','0802');
insert into book values(10011,'C程序设计','李洪',24,'清华大学出版社');
insert into book values(10012,'C程序设计','李洪',24,'清华大学出版社');
insert into book values(10013,'C习题解答','李洪',12,'清华大学出版社');
insert into book values(10014,'C习题解答','李洪',12,'清华大学出版社');
insert into book values(10020,'数据结构','徐华',29,'人民邮电出版社');
insert into book values(10021,'数据结构','徐华',29,'清华大学出版社');
insert into book values(10023,'高等数学','王涛',30,'高等教育出版社');
insert into book values(10034,'软件工程','张明',34,'机械工业出版社');
insert into book values(20025,'信息学','张港',35,'清华大学出版社');
insert into book values(20026,'信息学','张港',35,'清华大学出版社');
insert into book values(20042,'电工学','王民',30,'人民邮电出版社');
insert into book values(20056,'操作系统','曾平',26,'清华大学出版社');
insert into book values(20057,'操作系统','曾平',26,'清华大学出版社');
insert into book values(20058,'操作系统','曾平',26,'清华大学出版社');
insert into book values(20067,'数字电路','徐汉',26,'高等教育出版社');
insert into book values(20140,'数据库原理','陈曼',32,'高等教育出版社');
insert into book values(20090,'网络工程','黄军',38,'高等教育出版社');
select * from borrow;
insert into borrow values(1,10020,'2013-12-05');
insert into borrow values(1,20025,'2013-11-08');
insert into borrow values(1,20057,'2014-04-11');
insert into borrow values(2,10011,'2013-10-02');
insert into borrow values(2,10013,'2014-04-03');
insert into borrow values(3,10034,'2014-04-10');
insert into borrow values(3,20058,'2014-04-11');
insert into borrow values(4,10012,'2014-04-06');
insert into borrow values(5,10023,'2014-02-03');
insert into borrow values(10,20056,'2014-02-05');
insert into borrow values(12,20067,'2014-03-06');
上机实验2
use Library;
select count( distinct 图书名) as 图书品种数目 from book;
select 图书名,count( 图书名) as 图书数目 from book group by 图书名;
select 班号,count(班号) as 人数 from student group by 班号;
select d.系名,count(s.班号) as 人数 from student s join depart d on s.班号=d.班号 group by d.系名;
select s.学号,s.姓名,bo.图书名,b.借书日期 from student s join borrow b on b.学号=s.学号 join book bo on bo.图书编号=b.图书编号;
select distinct s.学号,s.姓名 from student s join borrow b on s.学号=b.学号;
select s.姓名,count(b.学号) 借书总数 from student s join borrow b on s.学号=b.学号 group by s.姓名;
select s.学号,s.姓名,count(b.学号) as 借书册数 from student s join borrow b on s.学号=b.学号
group by s.学号,s.姓名 having count(b.学号)>=2;
select s.学号,s.姓名,s.班号 from student s join borrow b on s.学号=b.学号
join book bo on bo.图书编号=b.图书编号 where bo.图书名='操作系统' ;
select s.班号,count(s.学号) 数目 from student s join depart d on s.班号=d.班号 join borrow b on s.学号=b.学号 group by s.班号;
select SUBSTRING(CAST(图书编号 as varchar(1000)),1,3) as 图书类别, cast(AVG(定价) as decimal(4,1)) as 平均价
from book group by SUBSTRING(CAST(图书编号 as varchar(1000)),1,3 );
select 图书编号,avg(定价) 平均价 from book group by 图书编号 having avg(定价)>=30
select 图书编号,avg(定价) as 平均价 ,max(定价) as 最高价 from book group by 图书编号;
select bo.图书编号,bo.图书名,s.学号,s.姓名 from student s join borrow b on s.学号=b.学号 join book bo on bo.图书编号
=b.图书编号 WHERE datediff(day,b.借书日期,'2014-1-20')>45;
select 图书编号,图书名,作者 from book where 图书名 like '%工程%';
select 图书名,作者 from book where 定价=(select max(定价) as 价格最高 from book);
select s.学号,s.姓名 from book b join borrow bo on b.图书编号=bo.图书编号 join student s on s.学号
=bo.学号 where 图书名='C程序设计' and not exists(select s.学号,s.姓名 from book b join borrow bo on b.图书编号=bo.图书编号 join student s on s.学号
=bo.学号 where 图书名!='C习题解答');
select 学号,姓名 from student where 学号 not in( select distinct 学号 from borrow);
select d.系名,count(b.学号) as 借阅图书数目 from student s join borrow b on s.学号=b.学号 join depart d on s.班号=d.班号
group by d.系名
select 出版社,count(图书编号) as 图书总数 from book group by 出版社;
select 出版社,cast(
round(
count(图书编号)*100.0/(select count(*) from book),
1)
as numeric(5,1)
) as 百分比
from book group by 出版社;
select b.出版社,count(bor.图书编号) as 借书数目 from
book b join borrow bor on bor.图书编号=b.图书编号 group by b.出版社;