Mysql 简单练习题(2)
-- 创建表readers 读者信息表
create table readers(
reader_id int(7) primary key,-- 读者id
name varchar(8),-- 读者姓名
sex varchar(2) default '男' check(sex in('男','女')),
dept varchar(16),-- 读者所在部门
status varchar(8),-- 学历
address varchar(30)-- 地址
);
-- 图书信息表
create table books(
book_id int(6) primary key,-- 图书编号
type_id varchar(3),-- 类型编号
book_name varchar(50),-- 图书名
author varchar(50),-- 作者
publisher varchar(50),-- 出版社
price int(3)-- 价格
);
-- 借阅信息表
create table borrow_info(
reader_id int(7),-- 读者id
book_id int(6),-- 图书Id
borrow_time date,-- 借书时间
notes varchar(200),-- 标志
primary key(reader_id,book_id)
);
alter table borrow_info add constraint fk_boorow_reader foreign key(reader_id) references readers(reader_id);
alter table borrow_info add constraint fk_boorow_books foreign key(book_id) references books(book_id);
-- 添加数据readers
insert into readers values('0034103','范元帅','男','艺术学院','本科生','师大南院');
insert into readers values('0034301','杨凡','男','电信学院','教师','八里台禄小区');
insert into readers values('0034429','许丹丹','女','艺术学院','本科生','师大南院');
insert into readers values('0134101','陈超','男','计算机学院','本科生','师大南院');
insert into readers values('0134102','范金良','男','计算机学院','本科生','师大南院');
insert into readers values('0134103','国皓','男','计算机学院','本科生','师大南院');
insert into readers values('0134104','贺云龙','男','计算机学院','本科生','师大南院');
insert into readers values('0134105','刘德文','男','计算机学院','本科生','师大南院');
insert into readers values('0134106','彭俊','男','计算机学院','本科生','师大南院');
insert into readers values('0134107','彭志成','男','计算机学院','本科生','师大南院');
insert into readers values('0134108','沈举','男','计算机学院','本科生','师大南院');
insert into readers values('0134109','田强','男','计算机学院','本科生','师大南院');
insert into readers values('0134110','王旭','男','计算机学院','本科生','师大南院');
insert into readers values('0134111','吴卫','男','计算机学院','本科生','师大南院');
insert into readers values('0134112','熊敏','男','计算机学院','本科生','师大南院');
insert into readers values('0134113','颜东','男','计算机学院','本科生','师大南院');
insert into readers values('0134114','殷建鹏','男','计算机学院','本科生','师大南院');
insert into readers values('0134115','周春林','男','计算机学院','本科生','师大南院');
insert into readers values('0134116','陈洁','女','计算机学院','本科生','师大南院');
insert into readers values('0134117','陈琪','女','计算机学院','研究生','师大南院');
insert into readers values('0134118','丁璐','女','计算机学院','研究生','师大南院');
insert into readers values('0134119','丁岩','女','计算机学院','本科生','师大南院');
insert into readers values('0134120','董蕾','女','计算机学院','本科生','师大南院');
insert into readers values('0134121','何芳','女','计算机学院','本科生','师大南院');
insert into readers values('0134122','蒋莎','女','计算机学院','本科生','师大南院');
insert into readers values('0134123','李颖','女','计算机学院','研究生','师大南院');
insert into readers values('0134124','李芬','女','计算机学院','研究生','师大南院');
insert into readers values('0134125','李锦萍','女','计算机学院','教授','师大南院');
insert into readers values('0134126','李小汐','女','计算机学院','本科生','师大南院');
insert into readers values('0134127','李玥玖','女','计算机学院','本科生','师大南院');
insert into readers values('0134128','郦莎','女','计算机学院','本科生','师大南院');
insert into readers values('0134129','廖英','女','计算机学院','本科生','师大南院');
insert into readers values('0134130','林琳','女','计算机学院','教师','师大南院');
insert into readers values('0134131','林敏','女','计算机学院','本科生','师大南院');
insert into books values('109101','BAS','高等数学','李方健','清华大学出版社','20');
insert into books values('109104','BAS','离散数学','孙德风','天津大学出版社','22');
insert into books values('109107','ELC','数字电路','刘国庆','高等教育出版社','13');
insert into books values('109109','CMP','C语言程序设计','谭浩强','清华大学出版社','15');
insert into books values('109110','CMP','数据结构','王志国','高等教育出版社','32');
insert into books values('109111','CMP','操作系统','王志国','高等教育出版社','25');
insert into books values('109112','CMP','计算机组成原理','张小敏','南大在学出版社','27');
insert into books values('109113','CMP','微机原理与接口技术','刘国庆','人民大学出版社','34');
insert into books values('109114','CMP','数据库原理','彭来德','高等教育出版社','16');
insert into books values('109115','CMP','计算机网络','马国露','人民大学出版社','11');
insert into books values('109116','CMP','计算机网络','谭浩强','南大在学出版社','13');
insert into books values('109117','CMP','编译原理','方刚','清华大学出版社','38');
insert into books values('209101','CMP','VB与WINDOWS程序设计','谭浩强','清华大学出版社','30');
insert into books values('209102','CMP','C++与面向对象技术','谭浩强','人民大学出版社','19');
insert into books values('209103','CMP','Java与网络程序设计','付勇','高等教育出版社','20');
insert into books values('209106','CMP','单片机原理及应用','刘国庆','人民大学出版社','22');
insert into books values('209107','CMP','PLC原理及其应用开发','刘国庆','南大在学出版社','16');
insert into books values('209111','CMP','人工智能导论','丁宝康','高等教育出版社','18');
insert into books values('209116','CMP','信息论与编码学概论','丁宝康','高等教育出版社','21');
insert into books values('209117','CMP','密码学基础','张顺志','人民大学出版社','25');
insert into books values('209123','CMP','Internet应用及网页设计','李朋','高等教育出版社','16');
insert into books values('209124','CMP','多媒体技术及应用','谭浩强','高等教育出版社','15');
insert into books values('209130','CMP','PB与数据库应用开发','张华强','清华大学出版社','42');
insert into books values('209132','CMP','计算机图形学','徐志超','人民大学出版社','27');
insert into books values('309102','CMP','计算机学科教学论','丁宝康','高等教育出版社','12');
insert into books values('309103','CMP','中学计算机教材研究与分析','徐志超','人民大学出版社','14');
insert into books values('309104','CMP','信息技术概论','付勇','天津大学出版社','24');
insert into books values('309105','CMP','数据库分析与设计','丁宝康','人民大学出版社','30');
insert into books values('309106','CMP','数据库导论','付勇','清华大学出版社','29');
7ff8
insert into borrow_info values('0034103','109101','2005-6-2 00:00:00','NULL');
insert into borrow_info values('0034301','209130','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0034429','109107','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134101','109109','2005-6-3 00:00:00','NULL');
insert into borrow_info values('0134102','109110','2005-6-30 00:00:00','NULL');
insert into borrow_info values('0134103','109111','2005-6-13 00:00:00','NULL');
insert into borrow_info values('0134104','109112','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134105','109113','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0134106','109114','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134107','109115','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134108','109116','2005-6-12 00:00:00','NULL');
insert into borrow_info values('0134109','109117','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134110','209101','2005-6-22 00:00:00','NULL');
insert into borrow_info values('0134111','209102','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134112','209103','2005-6-16 00:00:00','NULL');
insert into borrow_info values('0134113','209106','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134114','209107','2005-6-11 00:00:00','NULL');
insert into borrow_info values('0134115','209111','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134116','209106','2005-6-14 00:00:00','NULL');
insert into borrow_info values('0034301','209132','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0034301','309102','2005-6-9 00:00:00','NULL');
insert into borrow_info values('0034301','309103','2005-6-21 00:00:00','NULL');
insert into borrow_info values('0034301','309104','2005-6-14 00:00:00','NULL');
insert into borrow_info values('134101','109101','2005-6-1 00:00:00','NULL');
insert into borrow_info values('134102','109104','2005-6-8 00:00:00','NULL');
insert into borrow_info values('134104','109107','2005-6-1 00:00:00','NULL');
insert into borrow_info values('134105','109109','2005-6-1 00:00:00','NULL');
insert into borrow_info values('0134426','109110','2005-6-15 00:00:00','NULL');
insert into borrow_info values('0134427','109111','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134428','109112','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134429','109113','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','109114','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134431','109115','2005-6-1','NULL');
insert into borrow_info values('0134432','109116','2005-6-14','NULL');
insert into borrow_info values('0134433','109117','2005-6-1','NULL');
insert into borrow_info values('0134434','209101','2005-6-2','NULL');
insert into borrow_info values('0134435','209102','2005-6-1','NULL');
insert into borrow_info values('0134430','209103','2005-6-5','NULL');
insert into borrow_info values('0134430','209106','2005-6-5','NULL');
insert into borrow_info values('0134430','209107','2005-6-21','NULL');
insert into borrow_info values('0134430','209111','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','209116','2005-6-5 00:00:00','NULL');
insert into borrow_info values('0134430','209124','2005-6-5 00:00:00','NULL');
-- 1、检索读者“杨凡”所在单位
select dept from readers where name = '杨凡';
-- 2、检索所有读者的全部信息
select * from readers;
-- 3、检索图书馆中所有藏书的书名和出版单位
select book_name , publisher from books ;
-- 4、检索“人民大学出版社”所有的书名和单价,结果按照单价降序排列
select book_name , price from books where publisher = '人民大学出版社' order by price desc;
-- 5、检索价格在10元至15元之间的图书的名称、作者、单价和分类号,
-- 结果按分类号和单价升序排列
select book_name , author , price , type_id from books where price between 10 and 15 ORDER BY type_id , price ;
-- 6、检索“人民大学出版社”和“清华大学出版社”的所有图书的名称和作者
select book_name , author from books where publisher in ( '人民大学出版社' ,'清华大学出版社' );
-- 7、检索书名以“数据库”开头的所有图书的书名和作者
select book_name , author from books where book_name like '数据库%';
-- 8、检索同时借了总编号为209116和209124两本图书的借书证号
select reader_id from borrow_info where reader_id in (
select reader_id from borrow_info where book_id = '209116'
) and book_id = '209124';
/*或*/
select a.reader_id from borrow_info a , borrow_info b where a.book_id = '209116' and b.book_id = '209124';
-- 9、检索所有借阅了图书的读者姓名和所在单位
select name , dept from readers join borrow_info on readers.reader_id = borrow_info.reader_id ;
-- 10、检索“扬凡”所借的所有图书的书名和借阅日期
select book_name , borrow_time from books join borrow_info on books.book_id = borrow_info.book_id where borrow_info.reader_id = (
select reader_id from readers where name = '杨凡'
);
-- 11、检索价格在20元以上且已经借出的图书,结果按单价降序排列
select books.* from books join borrow_info on books.book_id = borrow_info.book_id where price > 20 ORDER BY price desc;
-- 12、检索借阅了“C语言程序设计”一书的读者姓名和所在单位
SELECT name , dept FROM readers , borrow_info WHERE readers.reader_id = borrow_info.reader_id AND borrow_info.book_id = (
SELECT book_id FROM books WHERE book_name = 'C语言程序设计'
);
-- 13、检索与“杨凡”在同一天借阅了图书的读者的姓名和所在单位
select name , dept FROM readers , borrow_info where readers.reader_id = borrow_info.reader_id and borrow_time in (
select borrow_time from borrow_info where reader_id = (
select reader_id from readers where name = '杨凡'
)
) and name != '杨凡';
-- 14、检索藏书中比“高等教育出版社”的所有图书的单价更高的图书
select books.* from books where price > (
select MAX(price) from books where publisher = '高等教育出版社'
);
-- 15、检索藏书中所有与“数据库导论”或“数据库原理”在同一出版社出版的图书
select books.* from books where publisher in (
select publisher from books where book_name in ('数据库导论','数据库原理')
)and book_name not in ('数据库导论','数据库原理') ;
-- 16、求该图书馆藏书的总册数
select COUNT(book_id) from books;
-- 17、求“高等教育出版社”的图书中最高的价格、最低的价格以及平均价格
select MAX(price) , MIN(price) , AVG(price) from books where publisher = '高等教育出版社';
-- 18、求“计算机学院”当前借阅了图书的读者人数
select COUNT(distinct borrow_info.reader_id) from borrow_info , readers where readers.reader_id = borrow_info.reader_id and dept = '计算机学院';
-- 19、求各个出版社的最高价格、最低价格、平均价格
select MAX(price) , MIN(price) , AVG(price) from books GROUP BY publisher;
-- 20、分别求出各个单位当前借阅图书的读者人数
select COUNT(distinct borrow_info.reader_id) from borrow_info , readers where readers.reader_id = borrow_info.reader_id GROUP BY dept;
-- 21、求各个出版单位的册书、价格总额,并按总价降序排列,
select COUNT(book_id) , SUM(price) from books GROUP BY publisher ORDER BY SUM(price) desc
-- 22、检索当前至少借阅了5本图书的读者姓名和所在单位
select name , dept from readers join borrow_info on readers.reader_id = borrow_info.reader_id GROUP BY name having COUNT(borrow_info.book_id) >= 5;
select name,dept from readers where reader_id in(
select reader_id from borrow_infogroup by reader_id having count(book_id)>=5
);
-- 23、分别找出借书人数超过10个人的单位和人数
select dept , COUNT(distinct borrow_info.reader_id) from readers join borrow_info on readers.reader_id = borrow_info.reader_id GROUP BY dept having COUNT(borrow_info.book_id) >= 10;
-- 24、检索没有借阅任何图书的读者姓名和所在单位
select name , dept from readers where reader_id not in (
select reader_id from borrow_info
);
- 安装linux版qq,安装二进制包编译器,安装mysql-5.6.11,删除已安装或安装失败的mysql-5.6.11,简单mysql练习题
- MySQL 简单使用
- ubuntu 安装简单php环境(不包含mysql)
- mysql存储过程的简单使用
- 【高并发简单解决方案】redis缓存队列+mysql 批量入库+php离线整合
- Ubuntu 安装mysql和简单操作
- 03--MySQL自学教程:MySQL安装后的目录结构介绍和简单语法指令
- PHP学习笔记(3)--Mysql部分简单命令
- linux下 MYSQL安装运行的简单方法
- mysql查询语句练习题跟答案
- mysql查询语句练习题跟答案
- 大数据下的mysql--简单分区分表
- Mysql分表查询的简单实现
- Java通过JDBC进行简单的增删改查(以MySQL为例)
- MySQL删除表的时候忽略外键约束的简单实现
- MySQL主从同步的简单理解
- mysql事务的简单理解
- java实现的简单网页爬虫:Servlet + MySQL5.5(二)
- 为什么那些大牛使用 MySQL这么简单
- MySQL简单语法(2)