[Oracle]以 bs 用户登录 ORCL 数据库, 将下列表中的数据插入到数据库的相应表中。将 ISBN 为 978-7-121-18619-8 的图书的零售价格(retail) 修改为 30。

实验五  SQL 语句应用

一、 实验目的

(1) 掌握数据的插入(INSERT)、 修改(UPDATE) 和删除(DELETE) 操作。

(2) 掌握不同类型的数据查询(SELECT) 操作。

二、 实验要求

(1) 利用 INSERT 语句向图书销售系统表中插入数据。

(2) 利用 UPDATE 语句修改图书销售系统表中的数据。

(3) 利用 DELETE 语句删除图书销售系统表中的数据。

(4) 利用 SELECT 语句实现对图书销售系统数据的有条件查询、 分组查询、 连接查询、 子查询等。

三、 实验内容

(1) 以 bs 用户登录 ORCL 数据库, 将下列表中的数据插入到数据库的相应表中。

 ①customers表

create table customers(customer_id int primary key,name varchar(20),phone varchar(50),email varchar(50),address varchar(10),Code varchar(20));

 create sequence cust_seq start with 1 increment by 1 maxvalue 999999 nocycle cache 10;

 insert into customers values(cust_seq.nextval,'王牧','83823422','Wangmu@sina.com','北京','110010');

 insert into customers values (cust_seq.nextval,'李青','83824566','Liqing@sina. com','大连','116023');

 ②publishers表

create table publishers(publisher_id int primary key,name varchar(40),contact varchar(10),Phone varchar(30));

insert into publishers values(1, '电子工业出版社', '张芳','56231234');

insert into publishers values(2, '机械工业出版社', '孙翔','89673456');

 ③books表

create table books(ISBN varchar(40) primary key,title varchar(20),author varchar(10),pubdate date,publisher_id int,cost int,retail int,category varchar(10));

insert into books values('978-7-121-18619-8', '文化基础', '王澜',to_date('2010-1-1','yyyy-mm-dd'),2,35,28, '管理');

insert into books values('978-7-122-18619-8', 'Oracle', '孙风栋',to_date('2011-2-1','yyyy-mm-dd'),1,40,32, '计算机');

④orders表

create table orders(order_id int primary key,customer_id int,orderdate date,shipdate date,shipaddress varchar(10),shipcode varchar(20));

create sequence order_seq start with 1000 increment by 1 maxvalue 999999 nocycle cache 10;

insert into orders values (order_seq.nextval,1,to_date('2013-2-1', 'yyyy-mm-dd'),to_date('2013-2-5','yyyy-mm-dd'), '大连','116023');

 insert into orders values (order_seq.nextval,2,to_date('2013-3-1', 'yyyy-mm-dd'),to_date('2013-3-10','yyyy-mm-dd'), '大连','116023');

⑤orderitem表

create table orderitem(order_id int,item_id int,ISBN varchar(50),quantity int,primary key(order_id,item_id));

insert into orderitem values(1000,1,'978-7-121-18619-8',5);

insert into orderitem values(1000,2,'978-7-122-18619-8',20);

insert into orderitem values(1001,1,'978-7-121-18619-8',15);

 ⑥promotion表

create table promotion(gift_id int primary key,name varchar(20),minretail int,maxretail int);

insert into promotion values(1,'签字笔',100,150);

insert into promotion values(2,'笔记本',150,300);

insert into promotion values(3,'保温杯',300,500);

(2)将 ISBN 为 978-7-121-18619-8 的图书的零售价格(retail) 修改为 30。

update books set retail=30 where ISBN='978-7-121-18619-8';

(3)将订单号为 1000 的订单的发货日期修改为“2013-2-2”。

update orders set shipdate=to_date('2013-2-2','yyyy-MM-dd') where order_id=1000;

(4)查询 BOOKS 表中包含的所有图书列表。

 select * from books;

(5)列出 BOOKS 表中有图书类型非空的图书书名。

select title from books where category is not null;

(6)列出 BOOKS 表中每本书的书名和出版日期。 对 pubdate 字段使用 Publication Date 列标题。

 select title,pubdate "Publication Date" from books;

(7)列出 CUSTOMERS 表中每一个客户的客户号以及他们所在的地址。

select customer_id,address from customers;

(8)查询包含各个出版社的名称、联系人及出版社电话号码的信息。其中,联系人的列在显示的结果中重命名为Contact Person。

select name,contact "Contact Person",phone from publishers;

(9)查询下达了订单的每一个客户的客户号。

 select customer_id from orders where order_id is not null;

(10)查询 2013 年 3 月 1 日之后发货的订单。

select * from orders where shipdate>to_date('2013-3-1','yyyy-MM-dd');

(11)查询居住在北京或大连的客户, 将结果按姓名的升序排列。

select * from customers where address='北京'or address='大连'order by name asc;

(12)列出姓“王” 的作者编写的所有图书信息, 并将结果按姓名降序排序。

select * from books where author like '王%' order by author desc;

(13)查询“儿童” 类和“烹饪” 类的所有图书。

select * from books where category='儿童' or category='烹饪';

(14)查询书名的第二个字母是“A”、 第四个字母是“N” 的图书信息。

select * from books where title like '_A_N%';

(15)查询电子工业出版社在 2011 年出版的所有“计算机” 类图书的名称。

select title from books where publisher_id=
(select publisher_id from publishers where name='电子工业出版社')
and extract(year from pubdate)='2011' and category='计算机';

(16) 查询图书名称、 出版社名称、 出版社联系人的名称、 EMAIL 和电话号码。

select title,name,contact,phone from books b,publishers p 
where b.publisher_id=p.publisher_id;

(17)查询当前还没有发货的订单信息及下达订单的用户名, 查询结果按下达订单日期排序。

select o.*,c.name from customers c,orders o 
where c.customer_id=o.customer_id and 
shipdate>to_date('2013-3-1','yyyy-MM-dd') 
order by orderdate;

(18)查询已经购买了“计算机” 类图书的所有人的客户号和姓名。

select c.customer_id,name from 
customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and 
o.order_id=om.order_id and om.isbn=b.isbn 
and category='计算机';

(19)查询“王牧” 购买的图书的 ISBN 以及书名。

select b.isbn,title from customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and 
o.order_id=om.order_id and om.isbn=b.isbn 
and name='王牧';

(20)确定客户“张扬” 订购的图书的作者。

select author from 
customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and 
o.order_id=om.order_id and om.isbn=b.isbn 
and name='张扬';

(21)查询 CUSTOMERS 表中的每一个客户所下达的订单数量。

select c.customer_id,name,count(order_id) as quantity 
from customers c,orders o 
where c.customer_id=o.customer_id 
group by c.customer_id,name;

(22)查询每个出版社出版图书的平均价格、 最高价格、 最低价格。

select p.publisher_id,name,avg(retail) avgPrice,
max(retail) maxPrice,min(retail) minPrice 
from books b, publishers p 
where b.publisher_id=p.publisher_id  
group by p.publisher_id,name;

(23)统计每个客户购买图书的数量及总价钱。(假设均以零售价出售)

select c.customer_id,c.name,sum(quantity),sum(quantity*retail) 
from customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and 
o.order_id=om.order_id and om.isbn=b.isbn 
group by c.customer_id,c.name;

(24)查询比 1000号订单中图书数量少的其它订单信息。

select order_id,sum(quantity) 
from orderitem 
group by order_id 
having sum(quantity)<
(select sum(quantity) from orderitem where order_id=1000);

(25)查询所有客户及其订购图书的信息。

select c.*,b.* 
from customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id 
and o.order_id=om.order_id and om.isbn=b.isbn;

(26)查询没有订购任何图书的客户信息。

select c.* from customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and o.order_id=om.order_id 
and om.isbn=b.isbn and o.order_id is null;

(27)查询订购金额最高的客户信息。(假设均以零售价出售)

select * from 
(select c.customer_id,sum(retail*quantity) cost_total 
from customers c,books b,orders o,orderitem om 
where c.customer_id=o.customer_id and o.order_id=om.order_id 
and om.isbn=b.isbn group by c.customer_id 
order by cost_total desc) a,customers where rownum<2;

(28)查询名为“赵敏” 的客户订购图书的订单信息、 订单明细。

select o.* from customer c,orders o where 
c.customer_id=o.customer_id and c.name=’赵敏’;

 

四、问题解答及实验结果

五、实验体会与收获

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值