设为首页 收藏本站
查看: 1174|回复: 0

[经验分享] Oracle面试复习(二)

[复制链接]

尚未签到

发表于 2016-7-18 09:53:26 | 显示全部楼层 |阅读模式
  
  现有一个商店的数据库,记录顾客及其购物情况,由下面三个表组成:
  商品product(商品号productid,商品名productname,单价unitprice,商品类别category,供应商provider,上架日期startdate);
  顾客customer(顾客号customerid,姓名name,住址location);
  购买purchase(顾客号customerid,商品号productid,购买数量quantity);
  
  试用SQL语言完成下列功能:
  1. 建表:在定义中要求声明:
  (1) 每个表的主外码;
  (2) 顾客的姓名和商品名不能为空值;
  (3) 单价必须大于0,购买数量必须再0到20之间;
  
  
  

create table rxy_product (
product_id    varchar2(10) not null primary key,
product_name  varchar2(30) not null,
unitprice     number(8,2) default 0.00,
category      varchar2(20),
provider      varchar2(30),
startdate     date,
constraint RXY_UNITPRICE check ( unitprice>0 )
);
create table rxy_customer (
customer_id   varchar2(10) not null primary key,
customer_name varchar2(12) not null,
location      varchar2(50)
);
create table rxy_purchase (
customer_id  varchar2(10) not null,
product_id   varchar2(10) not null,
quantity     number(4,0) default 0,
constraint FK_PRODUCT foreign key(product_id) references rxy_product,
constraint FK_CUSTOMER foreign key(customer_id) references rxy_customer,
constraint RXY_QUANTITY check ( quantity>=0 and quantity<=20 )
);
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M01','佳洁士',8.00,'牙膏','宝洁' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M02','高露洁',6.50,'牙膏','高露洁' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M03','洁诺',5.00,'牙膏','联合利华' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M04','舒肤佳',3.00,'香皂','宝洁' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M05','夏士莲',5.00,'香皂','联合利华' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M06','雕牌',2.50,'洗衣粉','纳爱斯' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M07','中华',3.50,'牙膏','联合利华' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M08','汰渍',3.00,'洗衣粉','宝洁' );
insert into rxy_product (product_id,product_name,unitprice,category,provider) values( 'M09','碧浪',4.00,'洗衣粉','宝洁' );
insert into rxy_customer values( 'C01','Dennis','海淀' );
insert into rxy_customer values( 'C02','John','朝阳' );
insert into rxy_customer values( 'C03','Tom','东城' );
insert into rxy_customer values( 'C04','Jenny','东城' );
insert into rxy_customer values( 'C05','Rick','西城' );
insert into rxy_purchase values( 'C01','M01',3 );
insert into rxy_purchase values( 'C01','M05',2 );
insert into rxy_purchase values( 'C01','M08',2 );
insert into rxy_purchase values( 'C02','M02',5 );
insert into rxy_purchase values( 'C02','M06',4 );
insert into rxy_purchase values( 'C03','M01',1 );
insert into rxy_purchase values( 'C03','M05',1 );
insert into rxy_purchase values( 'C03','M06',3 );
insert into rxy_purchase values( 'C03','M08',1 );
insert into rxy_purchase values( 'C04','M03',7 );
insert into rxy_purchase values( 'C04','M04',3 );
insert into rxy_purchase values( 'C05','M06',2 );
insert into rxy_purchase values( 'C05','M07',8 );
/
--(1)求购买了供应商"宝洁"产品的所有顾客;
select customer_name
from rxy_product p,rxy_customer c,rxy_purchase pc
where p.product_id=pc.product_id
and pc.customer_id=c.customer_id
and p.provider='宝洁';
--(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名);
select customer_name from rxy_customer where customer_id in (
select customer_id from rxy_purchase
where product_id in
(select pc.product_id from rxy_customer c, rxy_purchase pc where c.customer_id = pc.customer_id and c.customer_name='Dennis')
group by customer_id
having count(product_id)>=
(select count(pc.product_id) from rxy_customer c, rxy_purchase pc where c.customer_id = pc.customer_id and c.customer_name='Dennis')
);
--(3)求牙膏卖出数量最多的供应商。
select rownum, a.* from (
select p.provider, sum(pc.quantity) s from rxy_product p, rxy_purchase pc
where pc.product_id = p.product_id and p.category='牙膏'
group by p.provider
order by s desc) a where rownum=1;
select provider , NUM
from ( select distinct pd.provider ,
(select sum(SUM_NUM) from (
select p.provider pp, (select sum(quantity) from rxy_purchase pc
where pc.product_id=p.product_id
) "SUM_NUM"
from rxy_product p
where category='牙膏'
)
where pp=pd.provider
) "NUM"
from rxy_product pd
where  category='牙膏'
order by NUM desc
)
where rownum=1;
--4 将所有的牙膏商品单价增加10%。
update rxy_product set unitprice=unitprice*1.1 where category='牙膏';
--5 删除从未被购买的商品记录。
select * from rxy_product where product_id not in(select distinct(product_id) from rxy_purchase);
select * from rxy_product p where not exists(select 1 from rxy_purchase pc where p.product_id = pc.product_id);
select * from rxy_product
where product_id in (
select p.product_id from rxy_product p
minus
select pc.product_id  from  rxy_purchase pc
);
   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-245677-1-1.html 上篇帖子: oracle 日期加减的函数 下篇帖子: oracle表字段说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表