四、演
1、内置系统
vi numbe vrow emp%rowtype begi --以下四行对应四个异常,测试请依次放 vi := 8/0; --vi := 'aaa' --select * into vrow from emp where empno = 1234567 --select * into vrow from emp exceptio when zero_divide the dbms_output.put_line('发生除数为零异常') when value_error the dbms_output.put_line('发生类型转换异常') when no_data_found the dbms_output.put_line('没有找到数据异常') when too_many_rows the dbms_output.put_line('查询出多行记录,但是赋值给了%rowtype一行数据变量') when others the dbms_output.put_line('发生了其它的异常' || sqlerrm) end
2、抛出系统异
--查询指定编号的员工,如果没有找到,则抛出系统
declar --1.声明一个变量 %rowtyp vrow emp%rowtype begi --查询员工信息,保存起 select * into vrow from emp where empno = 8000 --判断是否触发异常的条 if vrow.sal is null the --抛出系统异 raise_application_error(-20001,'员工工资为空') end if exceptio when others the dbms_output.put_line('输出了其它的异常' || sqlerrm) end
3、抛出自定义异
--查询指定编号的员工,如果没有找到,则抛出自定义 declar --1.声明一个变量 %rowtyp vrow emp%rowtype --2.声明一个自定义的异 no_emp exception begi --查询员工信息,保存起 select * into vrow from emp where empno = 8000 --判断是否触发异常的条 if vrow.sal is null the raise no_emp; --抛出自定义的异 end if exceptio when no_emp the dbms_output.put_line('输出了自定义异常') when others the dbms_output.put_line('输出了其它的异常' || sqlerrm) end
9.3、索
一、含
索引相当于是一本书的目录,能够提高我们的查询
二、
1、创建
create [UNIQUE]|[BITMAP] index 索引名 on 表名(列名1,列名2,...
2、修改索
--重命名 alter index 索引名称 rename to 新的名称 --合并 alter index 索引名称 coalesce --重建 alter index 索引名称 rebuild --修改 先删除,在创 3、删除索 drop index 索引名
三、演
1、创建
create index INX_CATEGORY_CNAME on category(cname
2、修改索
--重命名 alter index INX_CATEGORY_CNAME rename to INX_CATEGORY_CNAME_NEW --合并 alter index INX_CATEGORY_CNAME_NEW coalesce --重建 alter index INX_CATEGORY_CNAME_NEW rebuild --修改 先删除,在创 3、删除索 drop index INX_CATEGORY_CNAM
9.4、视
一、含
视图是对查询结果的一个封装,视图里面所有的数据,都是来自于它查询的那张表,视图本身不存储任何数据,但是可以修改原数据,但是不建议这样
二、
1、创建
create view 视图 as 查询语 [with read only]
2、修改视
create or replace view 视图 as 查询语 [with read only]
3、删除视
drop view 视图名
三、演
1、创建
create view view_emp select ename,job,mgr from emp
2、修改视
create or replace view view_emp select ename,job,mgr,deptno from emp
3、删除视
drop view view_em
9.5、同义
一、含
同义词就是别名的意思和视图的功能类似,就是一种映射
二、
1、创建同
create [public] synonym 同义词名称 for 对象的名
2、修改同义
create or replace [public] synonym 同义词名称 for 对象的名
3、删除同义
drop [public] synonym 同义词名
三、演
1、创建同
-- create synonym syno_emp for emp --调 select * from syno_emp
2、修改同义
-- create or replace synonym syno_emp_update for emp --调 select * from syno_emp_update
3、删除同义
drop synonym syno_emp_updat
9.6、游
一、含
游标是用来操作查询结果集,相当于是JDBC中ResultSet,它可以对查询的结果一行一行的
二、--第一步:定义
--第一种:普通游
cursor 游标名[(参数 参数类型)] is 查询语句
--第二种:系统引用游
游标名 sys_refcursor
--第二步:打开
--第一种:普通游
open 游标名[(参数 参数类型)]
--第二种:系统引用游
open 游标名 for 查询语句
--第三步:获取
fetch 游标名 into 变量
--第四步:关闭
close 游标名
三、演
1、普通游标
--输出指定部门下的员工姓名和declar
--1.声明游
cursor vrows(dno number) is select * from emp where deptno = dno --声明变 vrow emp%rowtype begi
--2.打开游标
open vrows(10)
--3.循环遍
loo fetch vrows into vrow exit when vrows%notfound dbms_output.put_line('姓名:' || vrow.ename || ' 工资: ' || vrow.sal) end loop
--4.关闭游
close vrows end
2、系统引用游标使
--输出员工表中所有的员工姓名和
declar
--1.声明系统引用游
vrows sys_refcursor --声明变 vrow emp%rowtype begi
--2.打开游
open vrows for select * from emp
--3.循环遍
loo fetch vrows into vrow exit when vrows%notfound dbms_output.put_line('姓名:' || vrow.ename || ' 工资: ' || vrow.sal) end loop
--4.关闭游
close vrows end
3、使用for循环输
--输出员工表中所有的员工姓名和 declar cursor vrows is select * from emp begi --自动定义变量vrow,自动打开游标,自动关闭游 for vrow in vrows loo dbms_output.put_line('姓名:' || vrow.ename || ' 工资: ' || vrow.sal || '工作:' || vrow.job) end loop end
9.7、存储过
一、含
存储过程实际上是封装在服务器上一段PLSQL代码片断,它已经编译好了,如果客户端调用存储过程,执行效率就会非常
二、
1、创建存储
create procedure 存储过程名称(参数名 in|out 参数类型,参数名 in|out 参数类型,.. is|a --声明部 begi --业务逻辑 end
2、修改存储过
create [or replace] procedure 存储过程名称(参数名 in|out 参数类型,参数名 in|out 参数类型,.. is|a --声明部 begi --业务逻辑 end
3、删除存储过
drop procedure 存储过程名
4、调用存储过
--方式 call 存储过程名称(...) --方式 declar beg 存储过程名称(...) end
三、演
1、创建存储
--给指定员工涨薪并打印涨薪前和涨薪后的
create procedure proc_update_sal(vempno in number,vnum in number i
--声明变
vsal number begi
--查询当前的工
select sal into vsal from emp where empno = vempno
--输出涨薪前的工
dbms_output.put_line('涨薪后:' || (vsal + vnum))
dbms_output.put_line('涨薪前:' || vsal)
--更新工
update emp set sal = vsal + vnum where empno = vempno
--输出涨薪后的工
--提交事
commit end
--给员工编号为7521的员工涨工资1
call proc_update_sal(7521, 10)
2、修改存储过
--给指定员工涨薪并打印涨薪前和涨薪后的
create or replace procedure proc_update_sal(vempno in number,vnum in number
i
--声明变
vsal number begi
--查询当前的工
select sal into vsal from emp where empno = vempno
--输出涨薪前的工
dbms_output.put_line('涨薪前:' || vsal)
--更新工
update emp set sal = vsal + vnum where empno = vempno
--输出涨薪后的工
dbms_output.put_line('涨薪后:' || (vsal + vnum))
--提交事
commit end
--给员工编号为7521的员工涨工资1
call proc_update_sal(7521, 10)
3、删除存储过
drop procedure proc_update_sa
9.8、函
一、含
函数实际上是封装在服务器上一段PLSQL代码片断,它已经编译好了,如果客户端调用存储过程,执行效率就会非常高效,它跟存储过程没有什么本质区别,存储过程能做的函数也能做,只不过函数有返
二、
1、创建
create function 函数名称(参数名 in|out 参数类型,参数名 in|out 参数类型,...) return 返回的参数 is|a --声明部 begi --业务逻辑 end
2、修改函
create [or replace] function 函数名称(参数名 in|out 参数类型,参数名 in|out 参数类型,...) return 返回的参数 is|a --声明部 begi --业务逻辑 end
3、删除函
drop function 函数名
4、调用函
--方式 select 函数名称(...) from dual --方式 declar 变量名 变量类型 begi 变量名 = 函数名称(...) end
三、演
1、创建 --查询指定员工的 / 参数 : 员工的编 返回 : 员工的年薪 * create function func_getsal(vempno number) return numbe i vtotalsal number begi select sal * 12 + nvl(comm, 0) into vtotalsal from emp where empno = vempno return vtotalsal end --查询员工编号为7788的 declar vsal number begi vsal := func_getsal(7788) dbms_output.put_line(vsal) end
2、修改函
--查询指定员工的 / 参数 : 员工的编 返回 : 员工的年薪 * create or replace function func_getsal(vempno number) return numbe i vtotalsal number begi select sal * 12 + nvl(comm, 0) into vtotalsal from emp where empno = vempno return vtotalsal end --查询员工编号为7788的 declar vsal number begi vsal := func_getsal(7788) dbms_output.put_line(vsal) end
3、删除函
drop function func_getsa
名;列21;型;型21;名;名21;型;列