oracle筆試題及答案

學識都 人氣:1.63W

  oracle筆試題及答案

oracle筆試題及答案

1.創建表空間neuspace,數據文件命名爲,存放在d:data目錄下,文件大小爲200MB,設爲自動增長,增量5MB,文件最大爲500MB。(8分)

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

2. 假設表空間neuspace已用盡500MB空間,現要求增加一個數據文件,存放在e:appdata目錄下,文件名爲appneudata,大小爲500MB,不自動增長。(5分)

答:alter tablespace neuspace add datafile ‘e:appdata’ 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 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 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分)

STUNO SNAME SEX BIRTHDAY EMAIL SCORE CLASSNO
從stuseq取值 tom 1979-2-3 14:30:25 89.50 1
從stuseq取值 jerry 默認值 2

答: into student values(val, ’tom’, ’男’, to_date(‘1979-2-3

14:30:25’, ’yyyy-mm-dd fmhh24:mi:ss’), ’, 89.50, 1);

into student (stuno, sname, classno) values(val, ’jerry’, 2);

8. 修改表student的數據,將所有一班的學生成績加10分。(4分)

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

9. 刪除表student的數據,將所有3班出生日期小於1981年5月12日的記錄刪除。(4分)

答: 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 o, e, e, e from student s, class c where sno=sno;

(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;

[next]

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    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   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和的區別?

1、TRUNCATE在各種表上無論是大的還是小的都非常快。如果有ROLLBACK命令DELETE將被撤銷,而TRUNCATE則不會被撤銷。

2、TRUNCATE是一個DDL語言而DELETE是DML語句,向其他所有的DDL語言一樣,他將被隱式提交,不能對TRUNCATE使用ROLLBACK命令。

3、TRUNCATE將重新設置高水平線和所有的索引。在對整個表和索引進行完全瀏覽時,經過TRUNCATE操作後的表比DELETE操作後的表要快得多。

4、TRUNCATE不能觸發觸發器,DELETE會觸發觸發器。

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

6、當表被清空後表和表的索引講重新設置成初始大小,而則不能。

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_sysdate,’year’):tow thsound six to_sysdate,’yyyy’) :2006

to_sysdate,’month’):8月 to_sysdate,’mm’):08

to_sysdate,’day’):星期4 to_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 into itemfile values(‘i201′,’washer’,spares’,100,50,250,12,30); exception when dup_primary_key then dbms__line(‘重複項編號-主鍵衝突’); end

第一種的使用方法:exception

when 異常名稱 then

異常處理代碼;

第三種的用法:if 條件 then

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

end if;

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

• 內存優化

• 操作系統優化

• 數據存儲的優化

• 網絡優化等方法

具體到不同的數據庫涉及到要調整不同的數據庫配置文件、不同的操作系統參數、網絡參數等等, 不同的數據庫不同