实验二 数据查询和更新

本文由用户“kevinboran”分享发布 更新时间:2021-11-21 07:12:40 举报文档

以下为《实验二 数据查询和更新》的无排版文字预览,完整格式请下载

下载前请仔细阅读文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

实验二 数据查询和更新

实验目的和要求

了解脚本文件的使用;

掌握SELECT语句在单表查询中的应用;

掌握复杂查询的使用方法;??

掌握多表连接的方法;

掌握子查询的使用方法

掌握SQL的数据更新操作

掌握视图的定义、查询和更新操作

实验内容

(1)找出所有客户、代理商和商品都在同一城市的三元组(cid, aid, pid)。

select cid,aid,pid

from agents,customers,products

where agents.city=customers.city and customers.city=products.city

(2)找出所有客户、代理商和商品不都在同一城市(可能有两个在同一城市)的三元组(cid, aid, pid)。

select cid,aid,pid

from agents,customers,products

where agents.city!=customers.city or customers.city!=products.city

/

(3)找出所有在同一城市的代理商的aid对

select a1.aid,a2.aid

from agents a1,agents a2

where a1.city=a2.city and a1.aid!=a2.aid

(4)找出同时订购了商品p01和p07的客户的cid值。(若找出客户的cname呢?)

select DISTINCT o1.cid

from orders o1,orders o2

where o1.pid='p01' and o2.pid='p07' and o1.cid=o2.cid

/

找cname:

select DISTINCT cname

from customers,orders

where orders.cid=customers.cid and orders.cid in(select DISTINCT o1.cid

from orders o1,orders o2

where o1.pid='p01' and o2.pid='p07' and o1.cid=o2.cid)

(5)统计各个产品的销售总量

select pid,sum(qty)

from orders

group by pid

/

(6)当某个代理商所订购的某样产品的总量超过1000时,打印出所有满足条件的产品和代理商的ID以及这个总量。

select pid,aid,sum(qty)

from orders

group by pid,aid

having sum(qty)>1000

/

找出订购了产品p05的顾客的名字。

select DISTINCT cname

from customers,orders

where customers.cid=orders.cid and orders.pid='p05'

/

(8) 检索满足以下条件的顾客-代理商的姓名对(cname,aname),其中的顾客cname通过代理商aname订了货。

select cname,aname

from customers,agents,orders

where agents.aid=orders.aid and customers.cid=orders.cid

/

(9) 找出至少被两个顾客订购的产品的pid值。

select pid

from o 内容过长,仅展示头部和尾部部分文字预览,全文请查看图片预览。 ity=a.city and c.cid=orders.cid

/

(26)创建custs视图

create view custs

as

select *

from customers

where discnt

以上为《实验二 数据查询和更新》的无排版文字预览,完整格式请下载

下载前请仔细阅读上面文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

图片预览