• 105.50 KB
  • 2022-04-22 11:50:03 发布

《Oracle数据库应用与开发》教材 课后习题参考答案.doc

  • 22页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'1.8习题:1.(1)关系型数据库管理系统,简称RDBMS。是在E.F.Codd博士发表的论文《大规模共享数据银行的关系型模型》基础上设计出来的。它通过数据、关系和对数据的约束三者组成的数据模型来存放和管理数据。RDBMS是SQL的基础,同样也是所有现代数据库系统的基础。(2)在关系型数据库中,标准二维表是基本的数据存储单元,里边存储了实体的属性和实体间的关系。2.关系型数据库中,表与表之间包括的三种关系是:一对一、一对多和多对多。(1)一对一:有两个表,表A和表B。表A中的一条记录在表B中有一条记录与之对应。反过来,表B中的一条记录在表A中也仅有一条记录与之对应。(2)一对多:有两个表,表A和表B。表A中的一条记录在表B中有多条记录与之对应。反过来,表B中的一条记录在表A中仅有一条记录与之对应。(3)多对多:有两个表,表A和表B。表A中的一条记录在表B中有多条记录与之对应。反过来,表B中的一条记录在表A中也有多条记录与之对应。3.Oracle10g提供了四个版本,分别是:Oracle10g标准版1、Oracle10g标准版、Oracle10g企业版、Oracle10g个人版。 1.(1)客户机/服务器结构:简称C/S结构,是软件系统的一种体系结构。数据库和数据库管理系统运行在服务器端,各种Oracle管理工具运行在客户机上,二者通过SQL*NET进行通信。(2)浏览器/服务器结构:简称B/S结构,是随着Web技术兴起后的一种新型软件系统结构。它通常分为三层:数据库服务器,用来存储和管理数据库;应用程序服务器,与数据库服务器进行交互完成各种数据处理工作;客户端浏览器,与应用程序服务器进行交互,负责提交数据处理请求并获得和显示结果。2.(1)SYS:该用户被默认创建并授予DBA角色,它是Oracle数据库中权限最大的管理员账号。数据库中所有数据字典的基本表和视图都被存储在SYS的方案中,这些基本表和视图对于Oracle数据库的操作非常重要。为了维护数据字典的正确性和完整性,SYS方案中的表只能由系统维护,不能被任何用户或数据库管理员修改,而且任何用户不能在SYS方案中创建表。(2)SYSTEM:被默认创建并授予DBA角色权限仅次于SYS。该用户创建和管理数据库中可显示管理信息的表或视图,以及被Oracle数据库应用和工具使用的各种数据库对象。(3)SYSMAN:该用户是企业管理的超级管理员账号,该账号能够创建和修改其他管理员账号,同时也能管理数据库实例。(4)DBSNMP:是Oracle数据库中用于智能代理(Intelligent Agent)的用户,用来监控和管理数据库相关性能。如果停止该用户,则无法提取相关的数据库信息。1.(1)方法一:点击“开始”—>“程序”—>“Oracle-”—>“ApplicationDevelopment”—>“SQLPlus”进入Windows环境下的图形窗口(2)方法二:点击“开始”—>“运行”—>输入“cmd命令”—>进入DOS环境下执行SQLPLUS命令也可以打开SQL*Plus工具。(3)方式三:在IE浏览器地址栏中输入http://主机名:端口号/isqlplus|http://主机IP地址:端口号/isqlplus2.6习题一、填空题1.数据、日志、控制、初始化参数数据控制初始化参数2.表空间、段、盘区、数据块盘区数据块3.数据段盘区4.用户进程服务进程后台进程用户进程服务进程5.数据重做日志6.修改过的数据数据文件7.重做日志缓冲区内的日志重做日志8.日志条目归档日志设备归档9.共享服务器专有服务器共享服务器共享服务器 一、简答题1.Oracle数据库的物理存储结构式从物理组成的角度分析一个数据库在存储介质上的实际构成,它是由操作系统组织和管理的,是Oracle数据库的外部存储结构。(1)数据文件:数据文件时Oracle数据库用来存储各种数据的地方,如表中的记录、索引数据、系统数据和临时数据等。(2)日志文件:在Oracle中,日志文件记录了用户对数据的修改信息(如增加、删除、修改),名字通常为READ*.LOG格式。(3)控制文件:控制文件时Oracle数据库的物理文件之一,它是一个很小的二进制文件,记录了数据库的名称、数据文件和连击日志文件的名称及位置、当前的日志序列号、表空间等信息。(4)参数文件:参数文件中记录着数据库名称、控制文件的路径、SGA的内存结构、可选的Oracle特性和后台进程的配置参数等信息。2.(1)Oracle数据库逻辑存储结构是Oracle数据库创建后利用逻辑概念来描述数据库内部数据的组织和管理形式。包括表空间、段、区和块四种。(2)表空间和数据文件的关系:数据库中的一个表空间至少包含一个或多个数据文件,而一个数据文件只能属于一个表空间。这种关联实现了数据库的逻辑存储结构和物理存储结构的统一。3. (1)数据库实例是用户访问数据库的中间层,是使用数据库的手段,它为用户访问数据库提供了必要的内存空间和多个Oracle进程,它是动态的、临时的。一个Oracle实例由内存空间和Oracle进程两部分组成。(2)数据库和数据库实例是相辅相成、缺一不可的。如果只有数据库的物理文件,只能说明有数据被存储在数据库中,但是无法直接访问。如果只有数据库实例,说明为数据库的使用做好了准备、可以访问数据库,但不知道要操作的数据在哪里。1.(1)客户端配置a)sqlnet.ora文件用于指定数据库连接账号的验证方式以及连接字符串的解析方式,也就是说通过该文件决定怎样查找一个连接中出现的连接字符串的定义。b)tnsnames.ora文件时Oracle客户端的另一个重要文件,用来定义客户端连接远程服务器时的主机字符串。(2)服务器端配置一、listener.ora文件作用于Oracle数据库服务器端,是Oracle监听服务的配置文件。当客户端向服务器提交连接请求时,由监听服务接受并对连接账号进行有效验证,验证通过后将用户进程交给服务进程处理。若该文件被破坏,将影响监听服务的正常使用。 3.5习题一、选择题1.C2.C3.B二、简答题1.一个方案只能被一个数据库用户拥有,并且方案名称与这个用户的名称相同。创建新用户时系统自动创建该用户的方案。数据库的每一个用户都拥有一个唯一的方案。该用户创建的方案对象默认被保存在自己的方案中。2.CREATEUSER创建新用户命令:(1)USER-NAME:新创建的用户名称。(2)PASSWORD:为新用户制定密码。(3)DEFAULTTABLESPACE:为新用户指定默认表空间。用来储存该用户创建的方案对象。(4)TEMPORARYTABLESPACE:为用户指定临时表空间,存储操作过程中产生的临时数据。(5)QUOAT:为新用户指定磁盘配额,表示该用户在指定的空间中可以占用的最大磁盘空间。(6)UNLIMITED:表示该用户可以使用无限大的空间。(7)PROFILE:指定新用户使用的配置文件。(8)PASSWORDexpire:表示新用户密码过期,登陆后需要给出新密码。(9)ACCOUNTlock/unlock:表示用户加锁/解锁状态。 其中,用户名和密码是必须有的。一、操作题1.CREATEUSERORA_TEMPIDENTIFIEDBYTEM;2.ALTERUSERORA_TEMPIDENTIFIEDBYORA;3.ALTERUSERORA_TEMPACCOUNTLOCK;4.5实验1.a)createtablestudent(snochar(10)primarykey,snamevarchar2(8),sexchar(2)default"男"check(sex="男"orsex="女"),birthdaydate,sdeptchar(20));b)createtablecourse(cnochar(10)primarykey,cnamechar(30)unique, ccreditnumber(3));a)createtablescore(snochar(10)referencesstudent(sno),cnochar(10)referencescourse(cno),gradenumber(3)check(grade>=0andgrade<=100),primarykey(sno,cno));2.a)altertablestudentadd(memovarchar2(200));b)altertablestudentmodify(memovarchar2(300));c)altertablestudentdropcolumnmemo;3.a)insertintostudentvalues("001","zhang","女","01-7月-1992","生活部");insertintostudentvalues("001","lisi","女","11-5月-1992","艺术部");insertintostudentvalues("002","wang",default,"21-8月-1992","外联部");b)insertintocoursevalues("1001","文学","88"); insertintocoursevalues("1002","文学","99");insertintocoursevalues("1001","经济","89");.a)insertintoscorevalues("001","1001","6");insertintoscorevalues("001","1001","7");insertintoscorevalues("002","1001","8");b)updatescoresetsno="003"wheregrade="8";updatescoresetcno="1003"wheresno="002";c)deletefromstudentwheresname="lisi";2.a)管理---方案---表---在“方案”和“对象名”文本框中输入要删除的方案名或表名,选择需要删除的表,单机“删除”。b)droptablestudent;4.6习题1.AB2.B3C4.A5.B6.C7A8C9.B10.A 5.5实验(1)select*fromscott.empwheredeptno=20;(2)selectempno,ename,deptnofromscott.empwherejob="CLERK";(3)select*fromscott.empwherecomm>sal;(4)select*fromscott.empwherecomm>sal*0.2;(5)select*fromscott.empwhere(deptno=10andjob="MANAGER")or(deptno=20andjob="CLERK");(6)select*fromscott.empwherejobnotin("MANAGER","CLERK")andsal>=2000;(7)select*fromscott.empwherecommisnotnull;(8)selectcount(empno),avg(sal)fromscott.emp;(9)select*fromscott.empwherecommisnullorcomm<100(10)select*fromscott.empwhere(sysdate-hiredate)/365<=2;(11)select*fromscott.empwhere(sysdate-hiredate)/365>=10;(12)selectinitcap(lower(ename))fromscott.emp;(13)select*fromscott.empwherelength(ename)=6;(14)select*fromscott.empwhereenamenotlike"%S%";(15)select*fromscott.empwhereenamelike"_M%";(16)selectsubstr(ename,1,3)fromscott.emp;(17)selectreplace(ename,"s","S")fromscott.emp;(18)selectename,hiredatefromscott.emporderbyhiredate;(19)selectename,job,salfromscott.emporderbyjobdesc,salasc; (1)selectename,extract(yearfromhiredate)as年份,extract(monthfromhiredate)as月份fromscott.emporderby月份,年份;(2)selectdeptno,count(*),avg(sal),avg((sysdate-hiredate)/365)fromscott.empgroupbydeptno;(3)selectcount(*),avg(sal)fromscott.empgroupbydeptno;(4)selectmin(sal),jobfromscott.empgroupbyjobhavingmin(sal)<3000;(5)selectdeptno,job,max(sal)fromscott.empgroupbydeptno,job;(6)selectcount(*),avg(sal)fromscott.empgroupbyjob;5.6习题1.C2.A3.A4.A5.C6.A7.B8.C9.A10.B 6.5习题(1)selectename,dnamefromscott.empt1innerjoinscott.deptt2ont1.deptno=t2.deptnowherejob="CLERK";(2)select*fromscott.empt1rightjoinscott.deptt2ont1.deptno=t2.deptno(3)select*fromscott.empt1leftjoinscott.deptt2ont1.deptno=t2.deptno(4)用子查询实现:select*fromscott.empwheredeptno=(selectdeptnofromscott.deptwheredname="SALES")用连接查询实现:select*fromscott.empt1innerjoinscott.deptt2ont1.deptno=t2.deptnowheret2.dname="SALES";注意两种实现方式,在行和列上的变化。(5)简单自连接selectt1.enameas员工姓名,t2.ename经理姓名fromscott.empt1,scott.empt2wheret1.mgr=t2.empno;(6)selectt1.enameas员工姓名,t2.ename经理姓名fromscott.empt1,scott.empt2wheret1.mgr=t2.empnoandt1.hiredatet2.deptno(8)selectt1.enameas员工姓名,t2.ename经理姓名fromscott.empt1,scott.empt2wheret1.mgr=t2.empnoandt1.deptno=10;(9)select*fromscott.empwheresal>2500unionselect*fromscott.empwherejob="ANALYST"(10)select*fromscott.empwheresal>2500intersectselect*fromscott.empwherejob="ANALYST"(11)select*fromscott.empwheresal>2500minusselect*fromscott.empwherejob="ANALYST";(12)select*fromscott.empwheresal>(selectavg(sal)fromscott.emp)(13)select*fromscott.empwherejob=(selectjobfromscott.empwhere ename="SMITH")(14)select*fromscott.empwheresal>(selectsalfromscott.empwhereename="SMITH")(15)selectename,salfromscott.empwheresal>all(selectsalfromscott.empwheredeptno=30)(16)select*fromscott.empwheredeptnoin(selectdeptnofromscott.empgroupbydeptnohavingcount(*)>5);(17)select*fromscott.deptwheredeptnoin(selectdeptnofromscott.empgroupbydeptnohavingmin(sal)>2000)(18)本题的关键是使用子查询作为数据源和查询的条件,可以从最里层的子查询开始理解select*fromscott.deptwheredeptnoin(selectdeptnofrom(selectdeptno,count(*)as人数fromscott.empgroupbydeptno)where人数=(selectmax(人数)from(selectdeptno,count(*)as人数fromscott.empgroupbydeptno)));(19)select*fromscott.deptwheredeptnoin(selectdeptnofromscott.empgroupbydeptnohavingcount(*)>=1) (20)本题关键是使用相关子查询做条件select*fromscott.empewheresal>(selectavg(sal)fromscott.empgroupbydeptnohavinge.deptno=deptno);(21)本题关键是使用两个子查询的结果进行内连接查询select*from((select*fromscott.empewheresal>(selectavg(sal)fromscott.empgroupbydeptnohavinge.deptno=deptno))t1innerjoin(selectavg(sal),deptnofromscott.empgroupbydeptno)t2ont1.deptno=t2.deptno);(22)select*fromscott.deptwheredeptnoin(selectdistinctdeptnofromscott.empwhereempnoin(selectdistinctmgrfromscott.emp));(23)select*fromscott.empt1,scott.deptt2wheret1.deptno=t2.deptnoandt1.deptnoin(selectdeptnofromscott.empgroupbydeptnohavingavg(sal)<2000) 7.10习题一、1.执行部分2.RAISE二、1.D2.B3.CE4.A5.B1.loop循环实现:declareinumberdefault1;snumberdefault0;beginloops:=s+i;i:=i+1;exitwheni>100;endloop;dbms_output.put_line("和是:"||s);end;whlie循环实现:declareinumberdefault1; snumberdefault0;beginwhilei<=100loops:=s+i;i:=i+1;endloop;dbms_output.put_line("和是:"||s);end;for循环实现:declaresnumberdefault0;beginforiin1..100loops:=s+i;endloop;dbms_output.put_line("和是:"||s);end;2.显示游标的for循环:declarecursorcur1isselectename,salfromscott.emp;begin forrecincur1loopdbms_output.put_line(rec.ename||""||rec.sal);endloop;end;隐式游标的for循环:beginforrecin(selectename,salfromscott.emp)loopdbms_output.put_line(rec.ename||""||rec.sal);endloop;end; 8.4习题一、选择题1.A2.C3.B4.D5.A二、编程题1.步骤一:授予system用户对scott.emp具有显示的查询权限sqlplussystem/abcdefalteruserscottaccountunlock;connectscott/tiger;grantselectonemptosystem;connectsystem/abcdef;步骤二:创建存储过程setserveroutputoncreateorreplaceprocedurep1(idinscott.emp.deptno%type)iscursorc1isselect*fromscott.empwheredeptno=id;beginforrecinc1loopdbms_output.put_line(rec.empno||""||rec.ename||""||rec.job);endloop;end; 步骤三:执行存储过程executep1(10);2.步骤一:授权connectscott/tiger;grantupdateonemptosystem;步骤二:创建函数connectsystem/abcdef;createorreplacefunctionf1returnnumberiscursorc2isselect*fromscott.empforupdate;rowsnumberdefault0;anumber(7,2);beginselectavg(sal)intoafromscott.emp;forrecinc2loopifrec.sal