1、用sql语句创建表

/*创建客户表Customers*/
Create table Customers(
Cid char(4) primary key,
Cname varchar(20) not null,
City varchar(20) not null
)/*创建代理人表Agents*/
Create table Agents(
Aid char(3) primary key,
Aname varchar(20) not null,
City varchar(20) not null
)/*创建产品表Products*/
Create table Products(
Pid char(3) primary key,
Pname varchar(20) not null,
Quantity int,
Price decimal(6,2)
)/*创建订单表Orders*/
Create table Orders(
Ord_no char(4) primary key,
Month smallint not null,
Cid char(4) foreign key references Customers(Cid),
Aid char(3) foreign key references Agents(Aid),
Pid char(3) foreign key references Products(Pid),
Qty int,
Amount decimal(6,2)
)insert into Customers(Cid,Cname,City)
values('C001','詹三','杭州');insert into Customers(Cid,Cname,City)
values('C002','王勇','上海');insert into Customers(Cid,Cname,City)
values('C003','李晓红','上海');insert into Customers(Cid,Cname,City)
values('C004','赵子凡','杭州');insert into Customers(Cid,Cname,City)
values('C006','钱立','南京');insert into Agents(Aid,Aname,City)
values('A01','赵龙','北京');
insert into Agents(Aid,Aname,City)
values('A02','张建国','深圳');
insert into Agents(Aid,Aname,City)
values('A03','李林','广州');
insert into Agents(Aid,Aname,City)
values('A04','陈娟','北京');
insert into Agents(Aid,Aname,City)
values('A05','林子','杭州');
insert into Agents(Aid,Aname,City)
values('A06','吴文俊','上海');insert into Products(Pid,Pname,Quantity,Price)
values('P01','笔袋',111400,5.50);
insert into Products(Pid,Pname,Quantity,Price)
values('P02','尺子',203000,0.50);
insert into Products(Pid,Pname,Quantity,Price)
values('P03','橡皮',150600,0.50);
insert into Products(Pid,Pname,Quantity,Price)
values('P04','水笔',125300,1.00);
insert into Products(Pid,Pname,Quantity,Price)
values('P05','铅笔',221400,1.00);
insert into Products(Pid,Pname,Quantity,Price)
values('P06','涂改液',123100,2.00);
insert into Products(Pid,Pname,Quantity,Price)
values('P07','水彩笔',100500,1.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1011',1,'C001','A01','P01',1000,5500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1012',2,'C001','A01','P01',1000,5500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1019',2,'C001','A02','P02',400,200.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1017',2,'C001','A06','P03',600,300.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1018',2,'C001','A03','P04',600,600.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1023',3,'C001','A04','P05',500,500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1022',3,'C001','A05','P06',400,800.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1025',4,'C001','A05','P07',800,800.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1013',1,'C002','A03','P03',1000,500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1026',5,'C002','A05','P03',800,400.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1015',1,'C003','A03','P05',1200,1200.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1014',3,'C003','A03','P05',1200,1200.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1021',2,'C004','A06','P01',1000,5500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1016',1,'C006','A01','P01',1000,5500.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1020',2,'C006','A03','P07',600,600.00);insert into Orders(Ord_no,Month,Cid,Aid,Pid,Qty,Amount)
values('1024',3,'C006','A06','P01',800,4400.00);

2、(1)查询订单数量在500-800的订单数量

select * from orders
where qty between 500 and 800;

(2)查询产品名称中含有“水”字的产品名称与单价。

select pname,price from products
where pname like '%水%';

(3)查询每个月的订单数、总订货数量以及总金额,要求赋予别名,并按月份降序排列。

select month,count(*)订单数,sum(qty)总订货数量,sum(amount)总金额  from orders
group by month
order by month desc ;

(4)查询姓王且名字为两个字的客户在1月份的订单情况,并按订货数量降序排列。
嵌套查询完成:

select * from orders
where cid in (select cid from customerswhere cname like '王_') and month=1
order by qty desc ;

连接查询完成:

select orders.* from orders,customers
where orders.Cid=Customers.cid and cname like '王_' and month=1
order by qty desc ;

(5)查询上海客户总订货数量超过2000的订货月份。

select month from orders
where cid in (select cid from customerswhere city ='上海')group by month having sum(qty)>2000;

(6)查询每个产品的产品编号、产品名称、总订货数量以及总金额。

select orders.pid,pname,sum(qty),sum(amount) from orders,products
where orders.pid=products.pid
group by Orders.pid,pname;

(7)查询没有通过北京的代理订购笔袋的客户编号与客户名称。

select distinct orders.cid,cname from orders,Customers,Agents,Products
where Customers.Cid=Orders.Cid and Agents.Aid=Orders.Aid and Products.pid=Orders.pidand agents.city !='北京' and pname!='笔袋';

select distinct orders.cid,cname from orders,Customers
where Customers.Cid=Orders.cid and aid not in(select aid from agents where city='北京')and pid not in(select pid from Products where pname='笔袋');

(8)查询这样的订单号,该订单的订货数量大于3月份所有订单的订货数量。
使用带All谓词的子查询

select Ord_no  from orders
where month <> 3 and qty > all(select qty from Orders where month='3');

使用聚集函数

select Ord_no  from orders
where month <> 3 and qty > (select max(qty) from Orders where month='3');

(9)向产品表中增加一个产品,名称为粉笔,编号为p20,单价为1.5,销售数量为25000

insert into products
values('P20','粉笔',25000,1.5);

(10)将所有单价大于1.00的产品单价增加10%

update Products
set Price=Price*1.1
where Price>1.00;

(11)将所有由上海代理商代理的笔袋的订货数量改为2000

update orders
set qty=2000
where aid in (select aid from Agents where city='上海')and pid in(select pid from products where pname='笔袋');

(12)将由A06供给C006的产品P01改为由A05供应,请做必要的修改。

update orders
set aid='A05'
where aid='A06' and pid='P01' and cid='C006' ;

(13)从客户关系中删除C006记录,并从供应情况中删除相应的记录。

delete from orders
where Cid='C006';
go
delete from Customers
where Cid='C006';

(14)删除3月份订购尺子的所有订单情况。

delete from orders
where pid in (select pid from Products where pname='尺子') and month=3;

(15)为上海的客户建立一个代理情况视图,包括代理人姓名,产品名称以及产品单价。

create view v_agent(代理人姓名,产品名称,产品单价)
as select Aname,pname,price
from Agents a,Products p,Orders o,Customers c
where c.city='上海' and a.aid=o.aid and p.pid=o.pid and c.cid=o.cid;

(16)创建一个视图,要求包含单价大于1.00的所有产品的产品名称、总订货数量以及总金额。

create view s1(产品名称,总订货数量,总金额)
as select pname,sum(qty),sum(amount)
from products,orders
where products.pid = orders.pid and price>1.00
group by pname;

感悟:在查询语句中,使用的最多的就是连接查询与嵌套查询,个人跟喜欢连接查询,因为结构化特征更明显,但也有不适用的时候,比如例7,当涉及的表太多时,结构化查询就显得麻烦,不如连接查询。exists和not exists很少很少使用,因为可以用别的语句替换,反而降低了出错概率,了解用法即可。注意:聚集函数要给出分类依据,及group by,今天有好几处查询少了group by。

数据库原理及应用 清华大学出版社 96页课后练习题相关推荐

  1. 基础爬虫——以豆丁网《编译原理》(清华大学出版社第二版)课后习题答案为例

    目录 寻找目标地址规律 写代码 寻找目标地址规律 目标地址:目标地址 在此之前,讲一个小技巧,要在IE浏览器中打开这个网址,在谷歌浏览器中打开这个网址是找不到答案图片网址的,Firefox没试过.IE ...

  2. 计算机tcpip网络原理与应用,清华大学出版社-图书详情-《TCP/IP网络编程原理与技术》...

    前言 随着Internet的发展,网络技术已经渗透到人们的生活和工作中.TCP/IP已经成为最流行的网络协议,且还在演变以满足未来的需要.在速度越来越快的计算机硬件和不断更新的软件发展的背后,TCP/ ...

  3. mysql删除选课为空的学生_数据库原理答案 西安电子科技大学

    数据库原理-西安电子科技大学出版社 姓名:陈俊昌 班级:10923337 杭州电子科技大学计算机学院 11 1)给学生表增加一属性Nation(民族),数据类型为varchar(20) alter t ...

  4. FFmpeg入门详解--音视频原理及应用:梅会东:清华大学出版社

    大家好,我的第一本书正式出版了,可以在京东各大店铺抢购哦. <FFmpeg入门详解--音视频原理及应用:梅会东:清华大学出版社> 京东自营链接:https://item.jd.com/13 ...

  5. 编译原理课后习题答案清华大学出版社第二版

    想看更多算法题,可以扫描上方二维码关注我微信公众号"数据结构和算法",截止到目前我已经在公众号中更新了500多道算法题,其中部分已经整理成了pdf文档,截止到目前总共有800多页( ...

  6. 数据库习题(教材:刘爽英清华大学出版社版)

    数据库习题(教材:刘爽英清华大学出版社版) 第一章数据库概述,第二章数据模型与系统结构 第三章关系数据库系统模型 第四章结构化查询语言SQL 第五章关系数据库的设计理论 第七章 数据库的保护技术 第一 ...

  7. 计算机色彩再现原理,清华大学出版社-图书详情-《计算机色彩原理及应用》

    随着计算机.互联网.移动网络等科学技术的发展及应用普及,数字化色彩信息处理技术在数字传媒领域获得了越来越广泛的应用,并且逐渐进入人们的生活领域,而由此产生的问题及所需要的应对也从以前的专业技术领域向大 ...

  8. python刘卫国答案第二_清华大学出版社-图书详情-《数据库技术与应用实践教程—SQL Server 2012》...

    前言 在信息社会,数据已经成为重要的资源.大数据时代改变了人类原有的生活和发展模式,也改变了人类认识世界和判断价值的方式.以数据库技术为基础的数据管理技术,可以对数据进行有效的收集.加工.分析与处理, ...

  9. mysql数据库应用与开发姜桂洪 课后答案_清华大学出版社-图书详情-《MySQL数据库应用与开发习题解答与上机指导》...

    前言 本书是<MySQL数据库应用与开发>(姜桂洪主编,2018年由清华大学出版社出版)的配套辅导教材.书中采用目前实际生产和教学领域内应用广泛的MySQL软件,利用内容丰富的习题答案.涵 ...

  10. 计算机成原理第2版,清华大学出版社-图书详情-《计算机组成原理(第2版)》...

    本书是作者在广泛参阅国内外同类优秀教材的基础上,集十余年的课程教学与实验经验精心编写而成的.本书提出的"使学生建立起在控制器控制之下的计算机整体概念,充分理解程序.指令.控制.操作之间的关系 ...

最新文章

  1. ICCV2021 | SMCA:即插即用的共同注意力模型,可使DETR收敛加速10倍
  2. WinCvs 操作参考手册
  3. Python中float(‘inf‘)代表什么意思?
  4. JavaScript算法(实例九)整数的置换 / 求s=a+aa+aaa+aaaa+aa...a的值 / 自守数
  5. SpringBoot指南(七)——SpringBoot整合Mybatis
  6. plt.subplot()和plt.subplots(),plt.gca(),
  7. Convert.ToString和ToString的区别
  8. kettle使用经验02
  9. 2021-08-31为什么随机森林能降低方差
  10. 捷讯fw300r虚拟服务器口号,迅捷FW300R开启UPnP功能
  11. win10游戏不能窗口化怎么办?全屏游戏切换窗口的方法
  12. api 与 implement 的区别
  13. 我的2016---悲喜交加的一年
  14. win10亮度无法调节,怎么处理
  15. 【企业微信实现免密登录以及发送消息(企业内部应用)】
  16. Excel中身份证号码验证,那些不得不说的事
  17. Pygame:播放声音和音效
  18. java object比较排序
  19. 在SQL中修改数据库名称
  20. 流体力学基础——流体静力学

热门文章

  1. 博士和民工做测试的区别
  2. Python数据类型——字符串详解(1)
  3. 注塑机数据采集(海天、住友、发那科、力劲、伊之密、恩格尔、泰瑞、佳明、双马、宁波通讯塑机、申达、海雄、海达、丰铁、大禹)直采串口通讯网络通讯方案
  4. Python爬取58足浴上网站信息
  5. 网站制作响应式网页设计与SEO优化
  6. 微信开源 PhxQueue:“三高“的分布式队列
  7. 酷参考:5月10日BTC及NFT相关重要指标分享
  8. fc oracle,ORACLE数据文件系统只读(FC中断引起)转载【xiaoyu】
  9. 股粮网:水果价格整体回落 36城数据显示零售价下跌5.2%
  10. 智慧武装三维电子沙盘系统