【数据库原理】实验二 数据查询和更新

  • 实验目的和要求
    1. 了解脚本文件的使用;
    2. 掌握SELECT语句在单表查询中的应用; 
    3. 掌握复杂查询的使用方法;   
    4. 掌握多表连接的方法;
    5. 掌握子查询的使用方法
    6. 掌握SQL的数据更新操作 
    7. 掌握视图的定义、查询和更新操作
  • 实验内容

1、使用脚本文件(create.sql)创建CAP数据库。

操作步骤:

  • 新建查询,将附录中的create.sql的内容输入到查询窗口中,并保存为create.sql
  • 分析并执行

2、使用T-SQL装载数据

  • 设置外围应用配置器
  • 新建查询,并输入以下语句:

--以下代码通过使用openrowset函数为特定目的导入数据

use CAP

go

Insert into customers select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[客户$]);

Insert into agents select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[代理商$]);

insert  into products select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[产品$]);

insert into orders select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[订单$]);

如果上述操作不能执行,则采用实验一中导入cap.xls的方式导入四个表中数据。

3、查询和更新操作

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

select  cid,aid,pid

from agents,customers,products

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

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

select  cid,aid,pid

from agents,customers,products

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

找出所有在同一城市的代理商的aid对。

select  a.aid,b.aid

from agents a,agents b

where a.city=b.city and a.aid!=b.aid

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

select distinct a.cid

from orders a,orders b

where a.pid='p01'or pid='p07'and a.chi=b.chi

统计各个产品的销售总量。

select pid,sum(qty)

from orders

group by pid

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

select aid,pid,sum(qty)

from orders

group by aid,pid

having sum(qty)>1000

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

select distinct cname

from customers,orders

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

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

select distinct cname,aname

from agents,customers,orders

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

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

select distinct pid

from orders

group by pid 

having count(distinct cid)>1

在customers表中插入一个新行。

  1. Insert into customers(cid,cname,city) values (‘c007’,’WinDix’,’Dallas’);

检索customers表中discnt值为空的行。

select*

from customers

where discnt=0

检索客户以及他们订购商品的详细信息。(用外联接)

        Select customers.cid,cname,ordno,month,cid,aid,pid pty,dollars

From customers left outer join product on (customers.cid=product.cid)

检索有关住在Duluth或Dallas的代理商的所有信息。(要求使用IN谓词实现)

select  ordno from orders        

where cid in (select cid from customers                  

where city = 'Duluth') and             

aid in (select aid from agents                  

where city = ‘Dallas')               

order by ordno

找出通过住在Duluth或Dallas的代理商订货的所有顾客的姓名和折扣率。(要求使用IN谓词实现)

select cname 姓名,discnt 折扣  

       from customers 

       where cid in ( 

       select distinct orders.cid客户编号

       from agents ag inner join orders ord 

       on ag.aid = orders.aid 

       where ag.city = 'Duluth' or ag.city = 'Dallas' 

       )  

求所有满足以下条件的顾客的cid值:该顾客的discnt的值小于任一住在Duluth的顾客的discnt值。

Select cid

From customers 

Where discnt<all

(select dicint

From cuseomers

Where city=’ Duluth’)

And city<>’ Duluth’

检索没有通过代理商a05订货的所有顾客的名字。

提示:可以使用not in 或 <>all方式实现。

Select sname

From agents

Where aid not in

(select aid

From agents

Where aid=a05)

检索一个包含顾客所在的或者代理商所在的城市的名称。(使用UNION实现)

Select city

From agents

Union

Select city

From customers

在orders表中插入一个新行。

Insert into orders(ordno,month,cid,aid,pid) values (1107,’aug’,‘c006’,’a04’,’p01’);

创建一个名为swcusts的表,它包含住在西南部的所有顾客,并向该表中插入所有来自Dallas或Austin的顾客。

 Create swcusts(cid char(4) not null

Cname varchar(13),city varchar(20),

Discnt real,primary key(cid))

Insert into swcusts

Select* from customers

Where city in(‘Dallas’,’Auston’)

将所有住在New York的代理商的佣金率提高10%。

Update agents

Set per=per+10

Where city=’New York’

        

删除所有住在New York的代理商。

Delete

From agents

Where city=’New Yourk’

创建一个agentorders视图,它扩展了表orders的行,包括订货的代理商的详细信息。

Creat view agentorders

As

Select ordno,momth,cid,order.aid,pid,qty,dollars,aname,city,per

From agents,orders

Where agent.aid=order.aid

利用agentorders视图查询代理商Brown的所有订单信息

Select*

From agentorders

Where aname=’Brown’

创建cacities视图,该视图列出表customers和表agents中所有配对的城市,其中该顾客通过该代理商订购了商品。

Creat view cacites

As

Select cid,cname,customers.city,agents.city

From customers,agents,orders

Where customers.cid=order.cid

And agents.aid=order.cid

创建custs视图

create view custs as select * from customers where discnt<=15.0 with check option;

对custs视图进行更新操作。

Update custs set discnt=discnt+4;

数据库备份

操作步骤:

  • 在“对象资源管理器”窗口中,右键单击要备份的数据库。在弹出菜单中选择“任务”,单击“备份”。
  • 在上面窗口中,选择备份类型为“完整”。
  • 单击“添加”按钮设置目标属性。(要求保持数据库备份到用户盘,如E盘或F盘,便于下次实验实施数据库还原的操作)

示例是保存到f:\backup文件夹下,备份文件名为cap.bak

  • 设置路径后,单击“确定”按钮。随后单击“备份”窗口中的“确定”按钮。这将在指定位置创建数据库的一个备份。

思考题

  1. 空值是如何处理的?(排序、分组、比较、使用集合函数等)
  2. 出现问题及解决方案

附录

1、脚本文件(create.sql)的内容

-- create.sql

-- SQL statements for table creation for CAP database

create database CAP

on

(name=cap_data,--数据文件的逻辑名称,注意不能与日志逻辑同名

filename='d:\sql_data\cap_data.mdf' ,--物理名称,注意路径必须存在

size=10,--数据初始长度为5M

maxsize=50,--最大长度为10M

filegrowth=5%)--数据文件每次增长5%

log on

( name=cap_log, 

filename='d:\sql_data\cap_log.ldf ' , 

size=2 , 

maxsize=5 , 

filegrowth=1)

go

use CAP

go

create table customers (cid char(4) not null, cname varchar(13),

  city varchar(20), discnt real, primary key(cid));

create table agents (aid char(3) not null, aname varchar(13),

  city varchar(20), per smallint, primary key (aid));

create table products (pid char(3) not null, pname varchar(13),

  city varchar(20), quantity integer, price money,

  primary key(pid));

create table orders (ordno integer not null, month char(3),

  cid char(4), aid char(3), pid char(3),

  qty integer, dollars money, primary key(ordno));

脚本文件(import.sql)的内容

Insert into customers select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[客户$]);

Insert into agents select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[代理商$]);

insert  into products select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[产品$]);

insert into orders select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[订单$]);

实验 基本数据查询 一、实验目的 1.熟悉大型数据库实验环境,以MS SQL SERVER为例。 2.掌握MS SQL SERVER的查询分析器的用法。 3.能够完成对单表的查询操作。 4.能够完成对多表的联合查询操作。 5.能够完成带数据聚合函数的查询实验内容 (1)以实验一中创建的数据库abc作为查询对象,完成如下查询要求: 1.查询2001年12月31日之后的销售情况,要求列出销售人员姓名、销售的产品名以及销售日期。 2.查询销售电冰箱的销售人员的最大年龄。 3.统计每个产品的销售总数量,要求只列出销售数量前3名的产品销售总数量。 4.查询销售人员的销售情况,包括有销售记录的销售人员没有销售记录的销售人员,要求列出销售人员姓名、销售的产品号、销售数量销售日期。 5.列出2000年1月1日以后销售总量第一的产品的名称生产厂家。 (2)以MS SQL SERVER的例子数据库pubs为查询对象,完成如下查询: 6.Pubs数据库:没有写过business或者popular_comp类型书籍的作者编号姓名 7.Pubs数据库查询出版物价格在20元以上的作者编号姓名 8.Pub数据库:版税大于80%且state=ks的作者信息 9.Pub数据库查询作者数量小于5的州 10.Pub数据库查询价格最高的书的作者,他写的所有书的名称 11.Pub数据库查询销量较少的10本书的作者编写的所有书的书名 12.Pubs数据库查询写了价格PRICE高于平均价的书,而且所在的州STATE=CA的作者名称,图书名称,价格。 注意:如果数据库服务器中的pubs例子数据库被破坏,请用SQL Server 2000 Sample Databases.rar中的instpubs.sql还原
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值