Oracle11g数据库基础教程课后习题答案.docx
《Oracle11g数据库基础教程课后习题答案.docx》由会员分享,可在线阅读,更多相关《Oracle11g数据库基础教程课后习题答案.docx(13页珍藏版)》请在冰豆网上搜索。
Oracle11g数据库基础教程课后习题答案
Oracle11g数据库基础教程
参考答案
第5章数据库存储设置与管理
P70.实训题
(8)为USERS表空间添加一个数据文件,文件名为USERS05.DBF,大小为50MB。
ALTERTABLESPACEUSERSADDDATAFILE
‘D:
\ORACLE\ORADATA\ORCL\%users05.dbf’SIZE50M;
(9)为EXAMPLE表空间添加一个数据文件,文件名为example05.dbf,大小为20MB。
ALTERTABLESPACEEXAMPLE
ADDDATAFILE‘D:
\ORACLE\ORADATA\ORCL\example05.dbf’SIZE20M;
(10)修改USERS表空间中的userdata05.dbf为自动扩展方式,每次扩展5MB,最大为100MB。
ALTERDATABASEDATAFILE
‘D:
\ORACLE\ORADATA\ORCL\%userdata05.dbf’AUTOEXTENDONNEXT5MMAXSIZE100M;
(14)为数据库添加一个重做日志文件组,组内包含两个成员文件,分别为redo5a.log和redo5b.log,大小分别为5MB。
ALTERDATABASEADDLOGFILEGROUP5
(‘D:
\ORACLE\ORADATA\ORCL\redo5a.log’,
‘D:
\ORACLE\ORADATA\ORCL\redo5b.log’)SIZE5M;
(15)为新建的重做日志文件组添加一个成员文件,名称为redo5c.log。
ALTERDATABASEADDLOGFILEMEMBER
‘D:
\ORACLE\ORADATA\ORCL\redo5c.log’TOGROUP5;
(16)将数据库设置为归档模式,并采用自动归档方式。
SHUTDOWNIMMEDIATESTARTUPMOUNT
ALTERDATABASEARCHIVELOG;
ALTERDATABASEOPEN;
ALTERSYSTEMARCHIVELOGSTART
(8)
ALTERTABLESPACEUSERS
ADDDATAFILE‘D:
\ORACLE\ORADATA\ORCL\userdata05.dbf’SIZE50M’;
(9)
ALTERTABLESPACEEXAMPLE
ADDDATAFILE‘D:
\ORACLE\ORADATA\ORCL\example05.dbf’SIZE20M’;
(10)
ALTERDATABASEDATAFILE‘D:
\ORACLE\ORADATA\ORCL\userdata05.dbf’AUTOEXTENDONNEXT5MMAXSIZE100M;
(14)
ALTERDATABASEADDLOGFILEGROUP5
(‘D:
\ORACLE\ORADATA\ORCL\redo05a.log’,
’D:
\ORACLE\ORADATA\ORCL\redo05b.log’)SIZE5M;
(15)
ALTERDATABASEADDLOGFILEMEMBER
‘D:
\ORACLE\ORADATA\ORCL\redo05c.log’TOGROUP5;
(16)
SHUTDOWNIMMEDIATE
STARTUPMOUNT
ALTERDATABASEARCHIVELOG;
ALTERDATABASEOPEN;
ALTERSYSTEMARCHIVELOGSTART
第6章数据库对象的创建与管理
2.实训题
(2)
Createtableexer_class(
CNOnumber
(2)primarykey,
CNAMEvarchar2(20),
NUMnumber(3)
)
Createtableexer_student(
SNOnumber(4)primarykey,
SNAMEvarchar2(10)unique,
SAGEnumber,
SEXchar
(2),
CNOnumber
(2)
)
(3)
Altertableexer_studentaddconstraintck_sagecheck(sage>0andsage<=100);
(4)
altertableexer_studentaddconstraintck_stucheck(sex='M'orsex='F')modifysexdefault'M'
(5)
Createuniqueindexind_cnameonexer_class(cname);
(6)
Createviews_cas
Selectsno,sname,sage,sex,o,cname,num
Fromexer_classcjoinexer_students
Ono=o;
(7)
Createsequencesequ1startwith100000001;
(8)
createtableexer_student_range(
snonumber(4)primarykey,
snamevarchar2(10),sagenumber,
sexchar
(2),cnonumber
(2))
partitionbyrange(sage)
(partitionpart1valueslessthan(20)tablespaceexample,
partitionpart2valueslessthan(30)tablespaceorcltbs1,
partitionpart3valueslessthan(maxvalue)tablespaceorcltbs2)
(9)
createtableexer_student_list(
snonumber(4)primarykey,
snamevarchar2(10),sagenumber,
sexchar
(2),cnonumber
(2))
partitionbylist(sex)
(partitionmanvalues('M')tablespaceorcltbs1,
partitionwomanvalues('F')tablespaceorcltbs2)
(10)
Createindexindonexer_student_range(sno)local;
第9章PL/SQL语言基础
1.实训题
(1)
declare
cursorc_empisselect*fromemployees;
begin
forv_empinc_emploop
dbms_output.put_line(v_emp.first_name||''||v_emp.last_name||''||
v_emp.employee_id||''||v_emp.salary||''||v_emp.department_id);
endloop;
end;
(2)
declare
v_avgsalemployees.salary%type;
begin
forv_empin(select*fromemployees)loop
selectavg(salary)intov_avgsalfromemployees
wheredepartment_id=v_emp.department_id;
ifv_emp.salary>v_avgsalthen
dbms_output.put_line(v_emp.first_name||''||v_emp.last_name||''||
v_emp.employee_id||''||v_emp.salary||''||v_emp.department_id);
endif;
endloop;
end;
(3)
declare
cursorc_empis
selecte.employee_ideid,e.last_nameename,
e.department_idedid,m.employee_idmid,m.last_namemname
fromemployeesejoinemployeesm
one.manager_id=m.employee_id;
v_empc_emp%rowtype;
begin
openc_emp;
loop
fetchc_empintov_emp;
exitwhenc_emp%notfound;
dbms_output.put_line(v_emp.eid||''||v_emp.ename||''||
v_emp.edid||''||v_emp.mid||''||v_emp.mname);
endloop;
closec_emp;
end;
(4)
declare
v_empemployees%rowtype;
begin
select*intov_empfromemployeeswherelast_name='Smith';
dbms_output.put_line(v_emp.employee_id||''||
v_emp.first_name||''||v_emp.last_name||''||
v_emp.salary||''||v_emp.department_id);
exception
whenno_data_foundthen
insertintoemployees(employee_id,last_name,salary,email,hire_date,
job_id,department_id)
values(2010,'Smith',7500,'smith@',
to_date('2000-10-5','yyyy-mm-dd'),'AD_VP',50);
whentoo_many_rowsthen
forv_empin(select*fromemployeeswherelast_name='Smith')loop
dbms_output.put_line(v_emp.employee_id||''||
v_emp.first_name||''||v_emp.last_name||''||
v_emp.salary||''||v_emp.department_id);
endloop;
end;
第10章PL/SQL程序设计
(1)创建一个存储过程,以员工号为参数,输出该员工的工资。
createorreplaceprocedurepro_showsal(
p_empnoemployees.employee_id%type)
as
v_salemployees.salary%type;
begin
selectsalaryintov_salfromemployees
whereemployee_id=p_empno;
dbms_output.put_line(v_sal);
exception
whenno_data_foundthen
dbms_output.put_line('thereisnotsuchanemployees');
end;
begin
pro_showsal(100);
end;
(2)创建一个存储过程,以员工号为参数,修改该员工的工资。
若该员工属于10号部门,则工资增加140元;若属于20号部门,则工资增加200元;若属于30号部门,则工资增加250元;若属于其他部门,则工资增长300元。
createorreplaceprocedurepro_updatesal(
p_empnoemployees.employee_id%type)
as
v_deptnoemployees.department_id%type;
v_incnumber;
begin
selectdepartment_idintov_deptnofromemployees
whereemployee_id=p_empno;
casev_deptno
when10thenv_inc:
=140;
when20thenv_inc:
=200;
when30thenv_inc:
=250;
elsev_inc:
=300;
endcase;
updateemployeessetsalary=salary+v_inc
whereemployee_id=p_empno;
exception
whenno_data_foundthen
dbms_output.put_line('thereisnotsuchanemployees');
end;
(5)创建一个包,包中包含一个函数和一个过程。
函数以部门号为参数,返回该部门员工的最高工资;过程以部门号为参数,输出该部门中工资最高的员工名、员工号。
createorreplacepackagepkg_emp
as
functionfunc_ret_maxsal(p_deptnonumber)returnnumber;
procedurepro_showemp(p_deptnonumber);
end;
createorreplacepackagebodypkg_emp
as
functionfunc_ret_maxsal(p_deptnonumber)
returnnumber
as
v_maxsalnumber;
begin
selectmax(salary)intov_maxsalfromemployees
wheredepartment_id=p_deptno;
returnv_maxsal;
end;
procedurepro_showemp(p_deptnonumber)
as
cursorc_empisselect*fromemployees
wheredepartment_id=p_deptnoand
salary=func_ret_maxsal(p_deptno);
begin
forv_empinc_emploop
dbms_output.put_line(v_emp.employee_id||''||
v_emp.salary);
endloop;
end;
end;
(6)创建一个包,包中包含一个过程和一个游标。
游标返回所有员工的信息;存储过程实现每次输出游标中的5条记录。
createorreplacepackagepkg_showemp
as
cursorc_empisselect*fromemployees;
procedureshow_fiveemp;
end;
createorreplacepackagebodypkg_showemp
as
procedureshow_fiveemp
as
v_empc_emp%rowtype;
begin
ifnotc_emp%isopenthen
openc_emp;
endif;
foriin1..20loop
fetchc_empintov_emp;
ifc_emp%notfoundthen
closec_emp;
exit;
endif;
dbms_output.put_line(v_emp.employee_id||''||
v_emp.first_name);
endloop;
end;
end;
begin
pkg_showemp.show_fiveemp;
end;
(7)在employees表上创建一个触发器,保证每天8:
00~17:
00之外的时间禁止对该表进行DML操作。
createorreplacetriggertrg_emp
beforeinsertorupdateordeleteonemployees
declare
begin
ifto_char(sysdate,'HH24:
MI')notbetween'08:
00'and'17:
00'then
raise_application_error(-20000,'此时间内,不允许修改EMPLOYEES表');
endif;
end;
(8)在employees表上创建一个触发器,当插入、删除或修改员工信息时,统计各个部门的人数及平均工资,并输出。
createorreplacetriggertrg_8
afterinsertorupdateordelete
onemployees
declare
cursorc_deptis
selectdepartment_id,avg(salary)avgsal,count(*)num
fromemployeesgroupbydepartment_id;
begin
forvinc_deptloop
dbms_output.put_line(v.department_id||''||
v.avgsal||''||v.num);
endloop;
end;
第13章安全管理
2.实训题
(1)
CREATEUSERusera_exerIDENTIFIEDBYuseraDEFAULTTABLESPACEUSERSQUOTA10MONUSERSACCOUNTLOCK;
(2)
CREATEUSERuserb_exerIDENTIFIEDBYuserb;
(3)
GRANTCREATESESSIONTOusera_exerWITHADMINOPTION;
GRANTSELECT,UPDATEONehr.employeesTOusera_exerWITHGRANTOPTION;
(4)
ALTERUSERusera_exerACCOUNTUNLOCK;
(5)
CONNECTusera_erer/usera
SELECT*FROMehr.employees;
UPDATEehr.employeesSETsalary=salary+100;
GRANTSELECT,UPDATEONehr.employeesTOuserb_exer;
(6)
REVOKECREATESESSIONFROMusera_exer;
GRANTCREATESESSIONTOusera_exer;
(7)
REVOKESELECT,UPDATEONehr.employeesFROMusera_exer;
GRANTSELECT,UPDATEONehr.employeesTOusera_exer;
(8)
CREATEROLErolea;
CREATEROLEroleb;
GRANTCREATETABLETOrolea;
GRANTINSERT,UPDATEONehr.employeesTOrolea;
GRANTCONNECT,RESOURCETOroleb;
(9)
GRANTrolea,rolebTOusera_exer;
(10)
CREATEPROFILEpwdfile
LIMITCONNECT_TIME30IDLE_TIME10FAILED_LOGIN_ATTEMPTS4PASSWORD_LIFE_TIME20PASSWORD_LOCK_TIME10
ALTERUSERusera_exerPROFILEpwdfile;
第14章备份与恢复
2.实训题
(1)对human_resource数据库进行冷备份。
(2)对human_resource数据库进行一次完全的热备份。
(3)备份human_resource数据库的控制文件。
(4)假定human_resource数据库丢失了数据文件users01.dbf,使用数据库热备份对数据库进行恢复,并验证恢复是否成功。
(8)使用expdp命令导出human_resource数据库的ehr模式下的所有数据库对象。
第15章备份与恢复
(4)假设2013-3-12日在数据库中执行了下列操作。
略略略(课本可看)(P254)
(5)利用闪回查询,查询15:
40:
10时exercise中的数据
(6)利用闪回版本查询,查询15:
35:
10~15:
42:
10之间sno=100的记录版本信息
(7)利用闪回表技术,将exercise表恢复到删除操作进行之前的状态
(8)执行“DROPTABLEexercise”语句然后利用闪回删除技术恢复exercise表
(9)将数据库中的闪回日志保留时间设置为3天(4320分钟)
(10)利用闪回数据库技术,将数据库恢复到创造表之前的状态
settimeon
createtableflash_table(
idNUMBERPRIMARYKEY,
nameCHAR(20)
);
insertintoflash_tablevalues(100,'jack');
commit;
insertintoflash_tablevalues(200,'king');
commit;
insertintoflash_tablevalues(300,'john');
commit;
select*fromflash_table;
selectcurrent_scnfromv$database;
updateflash_tablesetname='wang'whereid=100;
commit;
select*fromflash_table;
deletefromflash_tablewhereid=300;
commit;
select*fromflash_table;
altertableflash_tableENABLEROWMOVEMENT;
flashbackt