1、例外处理
oracle将例外分为预定义例外,非预定定义例外和自定义例外三种。预定义例外用于处理常见的oracle错误,非预定定义例外用于处理预定义例外不能处理的例外,自定义例外用于处理与oracle错误无关的其他情况。

2、例外传递
如果不处理例外会出现什么情况呢?
案例:编写一个存储过程,可接受雇员的编号,并显示该雇员的姓名。如果输入的雇员编号不存在,怎么处理呢?
--这里编写一个块
declare
--定义变量
v_ename emp.ename%type;
begin
select ename into v_ename from emp where empno=&no;
dbms_output.put_line('名字:'||v_ename);
exception --例外处理
when no_data_found then
dbms_output.put_line('编号没有!');
end;

3、处理预定义例外
预定义例外是由pl/sql所提供的系统例外。当pl/sql应用程序违反了oracle规定的限制时,则会隐含的触发一个内部例外。pl/sql为开发人员提供了二十多种预定义例外。
(1)预定义例外 case_not_found
在开发pl/sql块中编写case语句时,如果在when子句中没有包含必须的条件分支,就会触发case_not_found的例外。
create or replace procedure ljy(spno number) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=spno;
case
when v_sal<1000 then
update emp set sal=sal+100 where empno=spno;
when v_sal<2000 then
update emp set sal=sal+200 where empno=spno;
end case;
exception
when case_not_found then
dbms_output.put_line('case语句没有与'||v_sal||'相匹配的条件');
end;

(2)预定义例外 cursor_already_open
当重新打开已经打开的游标时,会隐含的触发例外 cursor_already_open
declare
cursor emp_cursor is select ename,sal from emp;
begin
open emp_cursor;
for emp_record in emp_cursor loop
dbms_output.put_line(emp_record.ename);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('游标已经打开');
end;

(3)预定义例外 dup_val_on_index
在唯一索引所对应的列上插入重复的值时,会隐含的触发例外dup_val_on_index
begin
insert into dept values(10,'公关部','北京');
exception
when dup_val_on_index then
dbms_output.put_line('在deptno列上不能出现重复值');
end;

(4)预定义例外 invaild_cursor
当试图在不合法的游标上执行操作时,会触发该例外,例如:试图从没有打开的游标提取数据,或是关闭没有打开的则会触发该例外
declare
cursor emp_cursor is select ename,sal from emp;
emp_record emp_cursor%rowtype;
begin
open emp_cursor;--打开游标
fetch emp_cursor into emp_record;
dbms_output.put_line('emp_record.ename');
close emp_cursor;
exception
when invalid_cursor then
dbms_output.put_line('请检查游标是否打开');
den;

(5)预定义例外 invalid_number
当输入的数据有误时,会触发该例外。比如:数字100写成了1oo就会出发该例外
begin
update emp set sal=sal+'1oo';
exception
when invalid_number then
dbms_output.put_line('输入的数字不正确');
end;

//(6)预定义例外 no_data_found
(7)预定义例外 too_many_rows
当执行select into 语句时,如果返回超过了一行,则会触发该例外
declare
v_ename emp.ename%type;
begin
select ename into v_ename from emp;
exception
when too_many_rows then
dbms_output.put_line('返回了多行');
end;

(7)预定义例外zero_divide
当执行2/0 语句时,则会触发该例外。

(8)预定义例外 value_error
在执行赋值操作时,如果变量的长度不足以容纳实际数据,则会触发该例外value_error.比如:
declare
v_ename varchar2(5);
begin
select ename into v_ename from emp where empno=&no;
dbms_output.put_ine(v_ename);
exception
when value_error then
dbms_output.put_line('变量尺寸不足');
end;

4、处理自定义例外
预定义例外和自定义例外都是与oracle错误相关的,并且出现的oracle错误会隐含的出发相应额例外,而自定义例外与oracle错误没有任何关系,它是由开发人员为特定的情况所定义的例外。
案例:请编写一个pl/sql块,接受一个雇员的编号,并给该雇员工资增加1000元,如果该雇员不存在,请提示。
create or replace procedure ex_test(spno number) is
--定义例外
myexception exception;

begin
--更新用户sal
update emp set sal=sal+1000 where empno=spno;
--自定义例外
if sql%nofound then --sql%nofound表示没有执行update语句
raise myexception;--raise myexception表示触发myexception
end if;

exception
when myexception then
dbms_output.put_line('没有更新任何用户');

end;


5、oracle的视图 (当表结构过于复杂,请用视图吧)
视图是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。但是,视图并不在数据库中以存储的数据值形式存在。行和列数据来自有定义视图的查询所引用的表,并且在引用视图时动态生成。

(1)视图与表之间的区别
1)表要占用磁盘空间,视图不需要
2)视图不能添加索引
3)使用视图可以简化复杂查询。比如:学生选课系统
4)视图可以提高安全性。比如:不同用户查看不同视图

(2)创建视图
create view 视图名 as select语句【with read only】


(3)创建或修改视图
create or replace view 视图名 as select语句 【with read only】

(4)删除视图
drop view 视图名


案例:创建视图,把emp表的sal<1000的雇员映射到该视图中
create view myview as select * from emp where sal<1000;--视图创建成功,然后可以将myview授权给其他的用户。
--为了简化操作,用一个视图解决显示雇员编号,姓名和部门名称(这涉及到两个表查询)
create view myview as select emp.empno,emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno;