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.Oracle 10g提供了四个版本,分别是:Oracle 10g 标准版1、Oracle
10g 标准版、Oracle 10g 企业版、Oracle 10g 个人版。
4.
(1)客户机/服务器结构:简称C/S结构,是软件系统的一种体系
结构。数据库和数据库管理系统运行在服务器端,各种Oracle
管理工具运行在客户机上,二者通过SQL*NET进行通信。
(2)浏览器/服务器结构:简称B/S结构,是随着Web技术兴起后
的一种新型软件系统结构。它通常分为三层:数据库服务器,
用来存储和管理数据库;应用程序服务器,与数据库服务器进
行交互完成各种数据处理工作;客户端浏览器,与应用程序服
务器进行交互,负责提交数据处理请求并获得和显示结果。5.
(1)SYS:该用户被默认创建并授予DBA角色,它是Oracle数据
库中权限最大的管理员账号。数据库中所有数据字典的基本表
和视图都被存储在SYS的方案中,这些基本表和视图对于
Oracle数据库的操作非常重要。为了维护数据字典的正确性和
完整性,SYS方案中的表只能由系统维护,不能被任何用户或
数据库管理员修改,而且任何用户不能在SYS方案中创建表。
(2)SYSTEM:被默认创建并授予DBA角色权限仅次于SYS。该
用户创建和管理数据库中可显示管理信息的表或视图,以及被
Oracle数据库应用和工具使用的各种数据库对象。
(3)SYSMAN:该用户是企业管理的超级管理员账号,该账号能
够创建和修改其他管理员账号,同时也能管理数据库实例。
(4)DBSNMP:是Oracle数据库中用于智能代理(Intelligent Agent)
的用户,用来监控和管理数据库相关性能。如果停止该用户,则无法提取相关的数据库信息。
6.
(1)方法一:点击“开始”—>“程序”—> “Oracle - 称>”—>“Application Development”—>“SQL Plus” 进入 Windows环境下的图形窗口 (2)方法二:点击“开始”—>“运行”—>输入“cmd命令”—>进入 DOS环境下执行SQLPLUS命令也可以打开SQL*Plus工具。 (3)方式三:在IE浏览器地址栏中输入http://主机名:端口号 /isqlplus | http://主机IP地址:端口号/isqlplus 2.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)数据库和数据库实例是相辅相成、缺一不可的。如果只有数据 库的物理文件,只能说明有数据被存储在数据库中,但是无法 直接访问。如果只有数据库实例,说明为数据库的使用做好了 准备、可以访问数据库,但不知道要操作的数据在哪里。 4. (1)客户端配置 a)sqlnet.ora文件用于指定数据库连接账号的验证方式以及连 接字符串的解析方式,也就是说通过该文件决定怎样查找 一个连接中出现的连接字符串的定义。 b)tnsnames.ora文件时Oracle客户端的另一个重要文件,用来 定义客户端连接远程服务器时的主机字符串。 (2)服务器端配置 一、listener.ora文件作用于Oracle数据库服务器端,是 Oracle监听服务的配置文件。当客户端向服务器提交连接 请求时,由监听服务接受并对连接账号进行有效验证,验 证通过后将用户进程交给服务进程处理。若该文件被破坏, 将影响监听服务的正常使用。 3.5 习题 一、选择题 1. C 2. C 3. B 二、简答题 1.一个方案只能被一个数据库用户拥有,并且方案名称与这个用户 的名称相同。创建新用户时系统自动创建该用户的方案。数据库的每一个用户都拥有一个唯一的方案。该用户创建的方案对象默认被保存在自己的方案中。 2.CREATE USER创建新用户命令: (1)USER-NAME :新创建的用户名称。 (2)PASSWORD:为新用户制定密码。 (3)DEFAULT TABLESPACE: 为新用户指定默认表空间。用来储 存该用户创建的方案对象。 (4)TEMPORARY TABLESPACE: 为用户指定临时表空间,存储 操作过程中产生的临时数据。 (5)QUOAT: 为新用户指定磁盘配额,表示该用户在指定的空间中 可以占用的最大磁盘空间。 (6)UNLIMITED: 表示该用户可以使用无限大的空间。 (7)PROFILE: 指定新用户使用的配置文件。 (8)PASSWORD expire:表示新用户密码过期,登陆后需要给出新 密码。 (9)ACCOUNT lock/unlock : 表示用户加锁/解锁状态。 其中,用户名和密码是必须有的。 三、操作题 1.CREATE USER ORA_TEMP IDENTIFIED BY TEM; 2.ALTER USER ORA_TEMP IDENTIFIED BY ORA; 3.ALTER USER ORA_TEMP ACCOUNT LOCK; 4.5 实验 1. a)create table student( sno char(10) primary key, sname varchar2(8), sex char(2) default '男' check(sex='男' or sex='女'), birthday date, sdept char(20) ); b)create table course( cno char(10) primary key, cname char(30) unique, ccredit number(3) ); c)create table score( sno char(10) references student(sno), cno char(10) references course(cno), grade number(3) check(grade>=0 and grade<=100),primary key(sno,cno) ); 2. a)alter table student add (memo varchar2(200)); b)alter table student modify (memo varchar2(300)); c)alter table student drop column memo; 3. a) insert into student values('001','zhang','女','01-7月-1992','生活部'); insert into student values('001','lisi','女','11-5月-1992','艺术部'); insert into student values('002','wang',default,'21-8月-1992','外联部'); b) insert into course values('1001','文学','88'); insert into course values('1002','文学','99'); insert into course values('1001','经济','89');. c) insert into score values('001','1001','6'); insert into score values('001','1001','7'); insert into score values('002','1001','8'); d)update score set sno='003' where grade='8'; update score set cno='1003' where sno='002'; e)delete from student where sname='lisi'; 4. a)管理---方案---表---在“方案”和“对象名”文本框中输入要删除 的方案名或表名,选择需要删除的表,单机“删除”。 b)drop table student; 4.6 习题 1. AB 2. B 3 C 4. A 5. B 6. C 7 A 8 C 9. B 10. A 5.5 实验 (1)select * from scott.emp where deptno=20; (2)select empno,ename,deptno from scott.emp where job='CLERK'; (3)select * from scott.emp where comm>sal; (4)select * from scott.emp where comm>sal*0.2; (5)select * from scott.emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK'); (6)select * from scott.emp where job not in('MANAGER','CLERK') and sal>=2000; (7)select * from scott.emp where comm is not null; (8)select count(empno),avg(sal) from scott.emp; (9)select * from scott.emp where comm is null or comm<100 (10)select * from scott.emp where (sysdate-hiredate)/365<=2; (11)select * from scott.emp where (sysdate-hiredate)/365>=10; (12)select initcap(lower(ename)) from scott.emp; (13)select * from scott.emp where length(ename)=6; (14)select * from scott.emp where ename not like '%S%'; (15)select * from scott.emp where ename like '_M%'; (16)select substr(ename,1,3) from scott.emp; (17)select replace(ename,'s','S') from scott.emp; (18)select ename,hiredate from scott.emp order by hiredate; (19)select ename,job,sal from scott.emp order by job desc,sal asc; (20)select ename,extract(year from hiredate) as 年份,extract(month from hiredate) as 月份from scott.emp order by 月份,年份; (21)select deptno,count(*),avg(sal),avg((sysdate-hiredate)/365) from scott.emp group by deptno; (22)select count(*),avg(sal) from scott.emp group by deptno; (23)select min(sal),job from scott.emp group by job having min(sal)<3000; (24)select deptno,job,max(sal) from scott.emp group by deptno,job; (25)select count(*),avg(sal) from scott.emp group by job; 5.6 习题 1. C 2. A 3. A 4. A 5. C 6.A 7. B 8. C 9. A 10. B 6.5 习题 (1) select ename,dname from scott.emp t1 inner join scott.dept t2 on t1.deptno=t2.deptno where job='CLERK'; (2) select * from scott.emp t1 right join scott.dept t2 on t1.deptno=t2.deptno (3) select * from scott.emp t1 left join scott.dept t2 on t1.deptno=t2.deptno (4) 用子查询实现: select * from scott.emp where deptno=(select deptno from scott.dept where dname='SALES') 用连接查询实现: select * from scott.emp t1 inner join scott.dept t2 on t1.deptno=t2.deptno where t2.dname='SALES'; 注意两种实现方式,在行和列上的变化。 (5)简单自连接 select t1.ename as 员工姓名,t2.ename 经理姓名from scott.emp t1,scott.emp t2 where t1.mgr=t2.empno; (6) select t1.ename as 员工姓名,t2.ename 经理姓名from scott.emp t1,scott.emp t2 where t1.mgr=t2.empno and t1.hiredate (7) select t1.ename,t1.job,t1.deptno,t2.ename,t2.job,t2.deptno from scott.emp t1 cross join scott.emp t2 where t1.job=t2.job and t1.deptno <>t2.deptno (8) select t1.ename as 员工姓名,t2.ename 经理姓名from scott.emp t1,scott.emp t2 where t1.mgr=t2.empno and t1.deptno=10; (9) select * from scott.emp where sal>2500 union select * from scott.emp where job='ANALYST' (10) select * from scott.emp where sal>2500 intersect select * from scott.emp where job='ANALYST' (11) select * from scott.emp where sal>2500 minus select * from scott.emp where job='ANALYST'; (12) select * from scott.emp where sal>(select avg(sal) from scott.emp) (13) select * from scott.emp where job=(select job from scott.emp where ename='SMITH') (14) select * from scott.emp where sal>(select sal from scott.emp where ename='SMITH') (15) select ename,sal from scott.emp where sal>all(select sal from scott.emp where deptno=30) (16) select * from scott.emp where deptno in (select deptno from scott.emp group by deptno having count(*)>5); (17) select * from scott.dept where deptno in(select deptno from scott.emp group by deptno having min(sal)>2000) (18) 本题的关键是使用子查询作为数据源和查询的条件,可以从最里层的子查询开始理解 select * from scott.dept where deptno in (select deptno from (select deptno,count(*) as 人数from scott.emp group by deptno) where 人数=(select max(人数) from(select deptno,count(*) as 人数from scott.emp group by deptno))); (19) select * from scott.dept where deptno in(select deptno from scott.emp group by deptno having count(*)>=1) (20)本题关键是使用相关子查询做条件 select * from scott.emp e where sal>(select avg(sal) from scott.emp group by deptno having e.deptno=deptno); (21) 本题关键是使用两个子查询的结果进行内连接查询 select * from((select * from scott.emp e where sal>(select avg(sal) from scott.emp group by deptno having e.deptno=deptno)) t1 inner join (select avg(sal),deptno from scott.emp group by deptno) t2 on t1.deptno=t2.deptno); (22) select * from scott.dept where deptno in(select distinct deptno from scott.emp where empno in(select distinct mgr from scott.emp)); (23) select * from scott.emp t1,scott.dept t2 where t1.deptno=t2.deptno and t1.deptno in(select deptno from scott.emp group by deptno having avg(sal)<2000) 7.10 习题 一、 1.执行部分 2.RAISE 二、 1.D 2. B 3. CE 4. A 5. B 1. loop循环实现: declare i number default 1; s number default 0; begin loop s:=s+i; i:=i+1; exit when i>100; end loop; dbms_output.put_line('和是: '||s); end; whlie循环实现: declare i number default 1; s number default 0; begin while i<=100 loop s:=s+i; i:=i+1; end loop; dbms_output.put_line('和是: '||s); end; for循环实现: declare s number default 0; begin for i in 1..100 loop s:=s+i; end loop; dbms_output.put_line('和是: '||s); end; 2. 显示游标的for循环: declare cursor cur1 is select ename,sal from scott.emp; begin for rec in cur1 loop dbms_output.put_line(rec.ename||' '||rec.sal); end loop; end; 隐式游标的for循环: begin for rec in (select ename,sal from scott.emp) loop dbms_output.put_line(rec.ename||' '||rec.sal); end loop; end; 8.4 习题 一、选择题 1. A 2. C 3. B 4. D 5. A 二、编程题 1. 步骤一:授予system用户对scott.emp具有显示的查询权限sqlplus system/abcdef alter user scott account unlock; connect scott/tiger; grant select on emp to system; connect system/abcdef; 步骤二:创建存储过程 set serveroutput on create or replace procedure p1 (id in scott.emp.deptno%type) is cursor c1 is select * from scott.emp where deptno=id; begin for rec in c1 loop dbms_output.put_line(rec.empno||' '||rec.ename||' '||rec.job); end loop; end; 步骤三:执行存储过程 execute p1(10); 2. 步骤一:授权 connect scott/tiger; grant update on emp to system; 步骤二:创建函数 connect system/abcdef; create or replace function f1 return number is cursor c2 is select * from scott.emp for update; rows number default 0; a number(7,2); begin select avg(sal) into a from scott.emp; for rec in c2 loop