2022年紫川软件_数据库SQL试题-Oracle .pdf
数据库 SQL 1、 请将竖表 AA 转为横表BB ,字段及数据如下所示:转换前的竖表AA :ITEM_ID CONTENT CONTENT CONFIG 2040104 5 acct_item 2040104 6 serv_type 2040104 2 user_type 2040105 1 acct_item 2040105 2 serv_type 2040105 3 user_type 转换后的横表BB:ITEM_ID ACCT_ITEM SERV_TYPE USER_TYPE 2040104 5 6 2 2040105 1 2 3 SQL select decode(t.ITEM_ID,2040104,2040104,2040105,2040105)ITEM_ID, sum(decode(t.CONTENT_CONFIG,acct_item,t.CONTENT,0) ACCT_ITEM, sum(decode (t.CONTENT_CONFIG,serv_type,t.CONTENT,0) SERV_TYPE, sum(decode (t.CONTENT_CONFIG,user_type,t.CONTENT,0) USER_TYPE from Test t group by t.ITEM_ID;2、 删除表 CC 中的重复记录,CC 表结构如下:Column Name Data Type SERV_ID NUMBER ( 10 ) ACC_NBR VARCHAR2 ( 16 ) STATE CHAR ( 3 ) Delete from CC c1 , CC c2 where c1.serv_id!=c2.serv_id and c1.acc_nbr=c2.acc_nbr and c1.state=c2.state; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 3 页 - - - - - - - - - 3、 现有表 bill 和表 payment 结构如下,两表通过pay_id 关联:bill 表一一 bill_id 是帐单标识, charge 为帐单金额,pay_id 为付款编号,PK:bill_id bill_id NUMBER charge NUMBER pay_id NUMBER payment 表一 pay_id 为付款编号, charge 为付款金额,PK:pay_id pay_id NUMBER charge NUMBER 要求写出更新payment 使 charge-bill 表的同一pay_id 的 charge 之和的 SQL 语句。Update Payment p set p.charge =( select sum(b.charge) from bill b where p.pay_id = b.pay_id); 4、假设有当前员工工资表employee( ID , Name,department, salary) ,历史工资表his_employee( ID , Name , department , salary,update_date ) , update_date 为调薪日期,请用sql 完成以下工作:食堂的员工集体加薪5 %;选出历史上两年内调薪次数超过5 次的员工的ID,姓名,部门。5、写一个存储过程。已知条件:表person 的结构如下person_id number;person_name vachar2(50);city_code varchar2( 20 );phone_nbr varchar2( 20);is_vip char ( l ) ;现要求通过用户输入的city_code 和条件is_vip1 从表 person 中过滤数据,将过滤出来的数据打印输出,要求对person 表的扫描用游标方式实现。存储过程的起始定义如下,请按要求将程序补充完整。create or replace procedure SP_PROC_PERSON(IC_CITY_CODE IN VARCHAR2) IS 6、编写一个简单的Oracle 存储过程,输入参数必须有一个是结果集。Create or replace package mypack as Type test_cursor is ref cursor; Create or replace procedure sp_test (num in number , test_my_cursor mypack.test_cursor ) Is Begin Open test_cursor for select * from dual ; End ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 3 页 - - - - - - - - - 7、有 SQL 如下:select * from balance,balance_relation,serv where serv.serv_id=balance_relation.obj_id and balance_relation.obj_type= SERVand balance_relation.balance_id=balance.balance_id 其中 balance,balance_relation,serv 表的数据量都在600 万以上,不考虑表现有的主键情况,按照效率优先的原则,请写出该SQL 的优化方案。8、对以下的表创建数据库脚本。bill 表一 bill_id 为账单编号,charge 为账单金额,pay_id: 付款编号, bill_name: 账单描述,bill_date 账单日期PK:bill_id FK:pay_id, 对应 payment 表的主键。 Bill_date 有约束条件,只能在20092010 年。以bill_name (升序)和bill_date( 降序 ) 创建所以bill_id NUMBER(20) charge NUMBER(20,2) pay_id NUMBER(20) bill_name VARCHAR2(10) bill_date DATE payment 表一 pay_id 为付款编号, charge 为付款金额,PK:pay_id pay_id NUMBER(20) charge NUMBER(20,2) Create table bill( Bill_id number (20) primary key , Charge number (20,2), Pay_id number (20) references payment (pay_id), Bill_name varchar2(10), Bill_date date check( to_char(bill_date,yyyy) between 2009 and 2010 ) ) Create table payment ( Pay_id number(20) prmary key , Charege number (20,2) ) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 3 页 - - - - - - - - -