【整理师】整理大公司的oracle笔试题附参考答案

更新时间:2019-09-01    来源:Oracle教程    手机版     字体:

【www.bbyears.com--Oracle教程】

nvl(name,0) 空则取0。 sustrb(manth,1,4)第一位开始取4位。 Decode(je,1,1,2),je为1的时候显示1,否则显示2.

select classno, avg(score) from student group by classno having avg(score) = (select max(avg(score)) from student group by classno);

创建表空间neuspace,数据文件命名为neudata.dbf,存放在d:data目录下,文件大小为200MB,设为自动增长,增量5MB,文件最大为500MB。(8分)

答:create tablespace neuspace datafile ‘d:dataneudata.dbf’ size 200m auto extend on next 5m maxsize 500m;

2. 假设表空间neuspace已用尽500MB空间,现要求增加一个数据文件,存放在e:appdata目录下,文件名为appneudata,大小为500MB,不自动增长。(5分)

答:alter tablespace neuspace add datafile ‘e:appdataappneudata.dbf’ size 500m;

3. 以系统管理员身份登录,创建账号tom,设置tom的默认表空间为neuspace。为tom分配connect和resource系统角色,获取基本的系统权限。然后为tom分配对用户scott的表emp的select权限和对SALARY, MGR属性的update权限。(8分)

答:create user tom identified by jack default tablespace neuspace;

Grant connect, resource to tom;

Grant select, update(salary, mgr) on scott.emp to tom;

4. 按如下要求创建表class和student。(15分)

属性类型(长度)默认值约束含义

CLASSNO数值 (2)无主键班级编号

CNAME变长字符 (10)无非空班级名称

属性类型(长度)默认值约束含义

STUNO数值 (8)无主键学号

SNAME变长字符 (12)无非空姓名

SEX字符 (2)男无性别

BIRTHDAY日期无无生日

EMAIL变长字符 (20)无唯一电子邮件

SCORE数值 (5, 2)无检查成绩

CLASSNO数值 (2)无外键,关联到表CLASS的CLASSNO主键班级编号

答:create table class

(classno number(2) constraint class_classno_pk primary key,

cname varchar2(10) not null);

create table student

(stuno number(8) constraint student_stuno_pk primary key,

sname varchar2(12) not null,

sex char(2) default ‘男’,

birthday date,

email varchar2(20) constraint student_email_uk unique,

score number(5,2) constraint student_score_ck check(score>=0 and score<=100),

classno number(2) constraint student_classno_fk references class(classno)

);

5. 在表student的SNAME属性上创建索引student_sname_idx(5分)

答:create index student_sname_idx on student(sname);

6. 创建序列stuseq,要求初值为20050001,增量为1,最大值为20059999。(6分)

答:create sequence stuseq increment by 1 start with 20050001 maxvalue 20059999 nocache nocycle;

7. 向表student中插入如下2行。(5分)

STUNOSNAMESEXBIRTHDAYEMAILSCORECLASSNO

从stuseq取值tom男1979-2-3 14:30:25tom@163.net89.501

从stuseq取值jerry默认值空空空2

答:insert into student values(stuseq.nextval, ’tom’, ’男’, to_date(‘1979-2-3

14:30:25’, ’yyyy-mm-dd fmhh24:mi:ss’), ’tom@163.net’, 89.50, 1);

insert into student (stuno, sname, classno) values(stuseq.nextval, ’jerry’, 2);

8. 修改表student的数据,将所有一班的学生成绩加10分。(4分)

答:update student set score=score+10 where classno=1;

9. 删除表student的数据,将所有3班出生日期小于1981年5月12日的记录删除。(4分)

答:delete from student where classno=3 and birthday > ’12-5月-81’;

10. 完成以下SQL语句。(40分)

(1) 按班级升序排序,成绩降序排序,查询student表的所有记录。

答:select * from student order by classno, score desc;

(2) 查询student表中所有二班的成绩大于85.50分且出生日期大于1982-10-31日的男生的记录。

答:select * from student where classno=2 and score>85.50 and birthday < ’31-10月-82’ and sex=’男’;

(3) 查询student表中所有三班成绩为空的学生记录。

答:select * from student where classno=3 and score is null;

(4) 表student与class联合查询,要求查询所有学生的学号,姓名,成绩,班级名称。(使用oracle与SQL 99两种格式)

答:select s.stuno, s.sname, s.score, c.cname from student s, class c where s.classno=c.classno;

(5) 按班级编号分组统计每个班的人数,最高分,最低分,平均分,并按平均分降序排序。

答:select classno, count(*), max(score), min(score), avg(score) from student group by classno order by avg(score) desc;

(6) 查询一班学生记录中所有成绩高于本班学生平均分的记录。

答:select * from student where classno=1 and score > (select avg(score) from student where classno=1);

(7) 统计二班学生中所有成绩大于所有班级平均分的人数。

答:select count(*) from student where classno=2 and score > all (select avg(socre) from student group by classno);

(8) 查询平均分最高的班级编号与分数。

答:select classno, avg(score) from student group by classno having avg(score) = (select max(avg(score)) from student group by classno);

(9) 查询所有学生记录中成绩前十名的学生的学号、姓名、成绩、班级编号。

答:select stuno, sname, score, classno from (select * from student order by score desc) where rownum<=10;

(10) 创建视图stuvu,要求视图中包含student表中所有一班学生的stuno, sname, score, classno四个属性,并具有with check option限制。

答:create view stuvu

as

select stuno, sname,score,classno from student where classno=1 with check option;

1、比较大小

select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; –取较小值

sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1

例如:

变量1=10,变量2=20

则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。

2、表、视图结构转化

现有一个商品销售表sale,表结构为:

month    char(6)      –月份

sell    number(10,2)   –月销售金额

现有数据为:

200001  1000

200002  1100

200003  1200

200004  1300

200005  1400

200006  1500

200007  1600

200101  1100

200202  1200

200301  1300

想要转化为以下结构的数据:

year   char(4)      –年份

month1  number(10,2)   –1月销售金额

month2  number(10,2)   –2月销售金额

month3  number(10,2)   –3月销售金额

month4  number(10,2)   –4月销售金额

month5  number(10,2)   –5月销售金额

month6  number(10,2)   –6月销售金额

month7  number(10,2)   –7月销售金额

month8  number(10,2)   –8月销售金额

month9  number(10,2)   –9月销售金额

month10  number(10,2)   –10月销售金额

month11  number(10,2)   –11月销售金额

month12  number(10,2)   –12月销售金额

结构转化的SQL语句为:

create or replace view

v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)

as

select

substrb(month,1,4),

sum(decode(substrb(month,5,2),’01′,sell,0)),

sum(decode(substrb(month,5,2),’02′,sell,0)),

sum(decode(substrb(month,5,2),’03′,sell,0)),

sum(decode(substrb(month,5,2),’04′,sell,0)),

sum(decode(substrb(month,5,2),’05′,sell,0)),

sum(decode(substrb(month,5,2),’06′,sell,0)),

sum(decode(substrb(month,5,2),’07′,sell,0)),

sum(decode(substrb(month,5,2),’08′,sell,0)),

sum(decode(substrb(month,5,2),’09′,sell,0)),

sum(decode(substrb(month,5,2),’10′,sell,0)),

sum(decode(substrb(month,5,2),’11′,sell,0)),

sum(decode(substrb(month,5,2),’12′,sell,0))

from sale

group by substrb(month,1,4);

79、CASE语句的用法?

Oracle用法很简单:

SELECT last_name, job_id, salary

CASE job_id

WHEN ‘IT_PROG’ THEN 1.10*salary

WHEN ‘ST_CLERK’ THEN 1.15*salary

WHEN ‘SA_REP’ THEN 1.20*salary

ELSE salary END “REVISED_SALARY”

FROM employees

80、 truncate和delete的区别?

1、TRUNCATE在各种表上无论是大的还是小的都非常快。如果有ROLLBACK命令DELETE将被撤销,而TRUNCATE则不会被撤销。

2、TRUNCATE是一个DDL语言而DELETE是DML语句,向其他所有的DDL语言一样,他将被隐式提交,不能对TRUNCATE使用ROLLBACK命令。

3、TRUNCATE将重新设置高水平线和所有的索引。在对整个表和索引进行完全浏览时,经过TRUNCATE操作后的表比DELETE操作后的表要快得多。

4、TRUNCATE不能触发触发器,DELETE会触发触发器。

5、不能授予任何人清空他人的表的权限。

6、当表被清空后表和表的索引讲重新设置成初始大小,而delete则不能。

7、不能清空父表。

81、 表空间如何扩展?并用语句写出?

两种扩展方式:

a) 增加数据文件

alter tablespace tablespace_name add datafile ‘’ xxMB

b) 扩展数据文件大小

alter database datafile ‘’ resize newMB

82、 表空间区管理方式?哪种方式现在是推荐使用的?

a) 字典管理方式

extent management dictionary;默认方式

b) 本地管理方式

extent management local[autoallocate/uniform xxmb];

83、 用什么函数获得日期?和日期中的月,日,年

to_char(sysdate,’year’):tow thsound six to_char(sysdate,’yyyy’) :2006

to_char(sysdate,’month’):8月 to_char(sysdate,’mm’):08

to_char(sysdate,’day’):星期4 to_char(sysdate,’dd’):22

84、 分区表的应用?

a) 一个分区表有一个或多个分区,每个分区通过使用范围分区、散列分区、或组合分区分区的行

b) 分区表中的每一个分区为一个段,可各自位于不同的表空间中

c) 对于同时能够使用几个进程进行查询或操作的大型表分区非常有用

85、 谈谈索引的用法及原理?

索引是若干数据行的关键字的列表,查询数据时,通过索引中的关键字可以快速定位到要访问的记录所在的数据块,从而大大减少读取数据块的I/O次数,因此可以显著提高性能。

86、 存储过程的应用,如何既有输入又有输出?

Create procedure pro_name

(xxxx in/out type;

yyyy in/out/inout type;

) is/as

zzzz type;

begin

sqlpro;

exception

exceptionxxxxx;

commit;

end;

87、 常发生的异常有哪些?

常用预定义例外

CURSOR_ALREADY_OPEN — ORA-06511 SQLCODE = -6511 游标已经打开

DUP_VAL_ON_INDEX — ORA-00001 SQLCODE = -1 违反唯一性约束

INVALID_CURSOR — ORA-01001 SQLCODE = -1001 非法游标操作

INVALID_NUMBER — ORA-01722 SQLCODE = -1722 字符向数字转换失败

LOGIN_DENIED — ORA-01017 SQLCODE = -1017

NO_DATA_FOUND — ORA-01403 SQLCODE = +100 没有找到数据

NOT_LOGGED_ON — ORA-01012 SQLCODE = -1012 没有连接到数据库

PROGRAM_ERROR — ORA-06501 SQLCODE = -6501 内部错误

STORAGE_ERROR — ORA-06500 SQLCODE = -6500

TIMEOUT_ON_RESOURCE — ORA-00051 SQLCODE = -51

TOO_MANY_ROWS — ORA-01422 SQLCODE = -1422 返回多行

TRANSACTION_BACKED_OUT — ORA-00061 SQLCODE = -61

VALUE_ERROR — ORA-06502 SQLCODE = -6502 数值转换错误

ACCESS_INTO_NULL试图为NULL对象的属性赋值

ZERO_DIVIDE — ORA-01476 SQLCODE = -1476 被零除

OTHERS — 其它任何错误的处理

88、 如何使用异常?

在oracle中有三种类型的异常。预定义的异常 非预定义的异常 用户定义的异常 第二种非预定义的异常是与特定的oracle错误关联。并且用PRAGM EXCEPTION_INIT(EXCEPTION_NAME,ERROR_NUMBER)关联一起的。但是到底有什么用啊? 例如:declare dup_primary_key exception; pragma exception_init(dup_primary_key,-1); begin insert into itemfile values(‘i201′,’washer’,"spares’,100,50,250,12,30); exception when dup_primary_key then dbms_output.put_line(‘重复项编号-主键冲突’); end

第一种的使用方法:exception

when 异常名称 then

异常处理代码;

第三种的用法:if 条件 then

raise_application_error(-20000“““`-20999,提示信息);

end if;

89、优化的策略一般包括:

• 内存优化

• 操作系统优化

• 数据存储的优化

• 网络优化等方法

具体到不同的数据库涉及到要调整不同的数据库配置文件、不同的操作系统参数、网络参数等等, 不同的数据库不同

本文来源:http://www.bbyears.com/shujuku/65507.html