Oracle-SQL语句应用
SQL语句应用
这一次的一些操作也是基于之前建的数据库和表,具体可参见之前的博客
下面通过一些简单的示例来对一些简单的SQL语句进行一个应用,来了解Oracle数据库中数据的插入(INSERT)、修改(UPDATE)和删除(DELETE)操作,和不同类型的数据查询(SELECT)操作。
- (1)以bs用户登录BOOKSALES数据库,将表A-7至A-12中的数据插入数据库的相应表中。
insert into customers values(SEQ_CUSTOMERS.nextval,'王牧','83823422','Wangmu@sina.com','北京','110010');commit;
insert into customers values(SEQ_CUSTOMERS.nextval,'李青','83824566','Liqing@sina.com','大连','116023');commit;
insert into publishers values('1','电子工业出版社','张芳','56231234');commit;
insert into publishers values('2','机械工业出版社','孙翔','89673456');commit;
insert into books values('978-7-121-18619-8','文化基础','王澜',to_date('2010-1-1','yyyy-mm-dd'),'2','35','28','管理');commit;
insert into books values('978-7-122-18619-8','Oracle','孙凤栋',to_date('2011-2-1','yyyy-mm-dd'),'1','40','32','计算机');commit;
insert into orders values(SEQ_ORDERS.nextval,'1',to_date('2013-2-1','yyyy-mm-dd'),to_date('2013-2-5','yyyy-mm-dd'),'大连','116023');commit;
insert into orders values(SEQ_ORDERS.nextval,'2',to_date('2013-3-1','yyyy-mm-dd'),to_date('2013-3-10','yyyy-mm-dd'),'大连','116023');commit;
insert into orderitem values('1000','1','978-7-121-18619-8','5');commit;
insert into orderitem values('1000','2','978-7-122-18619-8','20');commit;
insert into orderitem values('1001','1','978-7-121-18619-8','15');commit;
insert into promotion values('1','签字笔','100','150');commit;
insert into promotion values('2','笔记本','150','300');commit;
insert into promotion values('3','保温杯','300','500');commit;
- (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表中包含的所有图书列表。
alter session set nls_date_format='yyyy-mm-dd';
select * from books;
- (5)列出BOOKS表中有图书类型非空的图书书名。
select * 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;
- (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;
- (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)查询电子工业出版社在2012年出版的所有“计算机”类图书的名称。
select title from publishers,books where
publishers.name='电子工业出版社' and
to_char(books.pubdate, 'yyyy') = 2012 and
books.category='计算机' and
publishers.publisher_id = books.publisher_id;
//没有符合要求的
- (16)查询图书名称、出版社名称、出版社联系人的名称和电话号码。
select title,publishers.name,contact,phone from publishers,books where publishers.publisher_id = books.publisher_id;
- (17)查询当前还没有发货的订单信息及下达订单的用户名,查询结果按下达订单日期排序。
select order_id,orders.customer_id,orderdate,shipdate,shipaddress,shipcode,name from customers,orders where
to_date(orders.shipdate, 'yyyy-mm-dd') > to_date(sysdate, 'yyyy-mm-dd') and
orders.customer_id = customers.customer_id order by orderdate;
//没有符合要求的
- (18)查询已经购买了“计算机”类图书的所有人的客户号和姓名。
select orders.customer_id,name from customers,books,orders,orderitem where
books.category='计算机' and books.isbn=orderitem.isbn and
orderitem.order_id=orders.order_id and orders.customer_id=customers.customer_id;
- (19)查询“王牧”购买的图书的ISBN以及书名。
select orderitem.isbn,books.title from customers,books,orders,orderitem where
customers.name='王牧' and customers.customer_id=orders.customer_id and
orders.order_id=orderitem.order_id and orderitem.isbn=books.isbn;
- (20)查询订购图书“Oracle”的客户将收到什么样的礼品。
select books.retail*orderitem.quantity as pay from books,orderitem where books.title='Oracle' and
books.isbn=orderitem.isbn;
- (21)确定客户“张扬”订购的图书的作者。
select author from customers,orders,orderitem,books where customers.name='张扬' and
customers.customer_id=orders.customer_id and orders.order_id=orderitem.order_id and
orderitem.isbn=books.isbn;
//没有符合要求的
- (22)查询CUSTOMERS表中每一个客户所下达的订单数量。
select customers.name,count(orders.order_id) from customers,orders,orderitem where
customers.customer_id=orders.customer_id and orders.order_id=orderitem.order_id group by customers.name;
- (23)查询价格低于同一种类图书的平均价格的图书信息。
select category,min(retail),max(retail),avg(retail) from books group by category;
- (24)查询每个出版社出版图书的平均价格、最高价格、最低价格。
select avg(retail),max(retail),min(retail) from books,publishers where books.publisher_id=publishers.publisher_id;
- (25)统计每个客户购买图书的数量及总价钱。
select customers.customer_id,sum(quantity),sum(quantity*cost) from customers,books,orders,orderitem where
customers.customer_id=orders.customer_id and orders.order_id=orderitem.order_id and
orderitem.isbn=books.isbn group by customers.customer_id;
- (26)查询比1000号订单中图书数量多的其他订单信息。
select order_id,sum(quantity) from orderitem group by order_id
having sum(quantity) > (select sum(quantity) from orderitem group by order_id
having order_id=1000);
//没有符合要求的
- (27)查询所有客户及其订购图书的信息。
select * from customers,orders,books,orderitem where customers.customer_id=orders.customer_id and
orders.order_id=orderitem.order_id and orderitem.isbn=books.isbn;
- (28)查询没有订购任何图书的客户信息。
select customers.customer_id from customers,orders,orderitem where customers.customer_id=orders.customer_id
and orders.order_id=orderitem.order_id and orders.order_id is null;
//没有符合要求的
- (29)查询订购金额最高的客户信息。
select customers.customer_id,sum(quantity*cost) from customers,books,orders,orderitem where
customers.customer_id=orders.customer_id and orders.order_id=orderitem.order_id and orderitem.isbn=books.isbn
group by customers.customer_id having sum(quantity*cost)=975;
- (30)查询名为“赵敏”的客户订购图书的订单信息、订单明细。
select * from customers,orders where orders.customer_id = customers.customer_id and customers.name='赵敏';
//没有符合要求的
创作不易,喜欢的话加个关注点个赞,蟹蟹蟹蟹٩(‘ω’)و