当前位置:   article > 正文

Oracle中视图丶触发器丶存储过程/函数_oracle 视图 函数

oracle 视图 函数

视图

视图就是封装了一条复杂查询的语句。
语法 1.:CREATE VIEW 视图名称 AS 子查询

语法 2:CREATE OR REPLACE VIEW 视图名称 AS 子查询

语法 3:CREATE OR REPLACE VIEW 视图名称 AS 子查询 WITH READ ONLY

  1. ---视图
  2. ---视图的概念:视图就是提供一个查询的窗口,所有数据来自于原表。
  3. ---查询语句创建表
  4. create table emp as select * from scott.emp;
  5. select * from emp;
  6. ---创建视图【必须有dba权限】
  7. create view v_emp as select ename, job from emp;
  8. ---查询视图
  9. select * from v_emp;
  10. ---修改视图[不推荐]
  11. update v_emp set job='CLERK' where ename='ALLEN';
  12. commit;
  13. ---创建只读视图
  14. create view v_emp1 as select ename, job from emp with read only;
  15. ---视图的作用?
  16. ---第一:视图可以屏蔽掉一些敏感字段。
  17. ---第二:保证总部和分部数据及时统一。

ps:物化视图:会在数据库中真正找到一张表,而普通视图不是

索引

索引是用于加速数据存取的数据对象。合理的使用索引可以大大降低 i/o 次数,从而
提高数据访问性能。

  1. ---索引
  2. --索引的概念:索引就是在表的列上构建一个二叉树
  3. ----达到大幅度提高查询效率的目的,但是索引会影响增删改的效率。
  4. ---单列索引
  5. ---创建单列索引
  6. create index idx_ename on emp(ename);
  7. ---单列索引触发规则,条件必须是索引列中的原始值。
  8. ---单行函数,模糊查询,都会影响索引的触发。
  9. select * from emp where ename='SCOTT'
  10. ---复合索引
  11. ---创建复合索引
  12. create index idx_enamejob on emp(ename, job);
  13. ---复合索引中第一列为优先检索列
  14. ---如果要触发复合索引,必须包含有优先检索列中的原始值。
  15. select * from emp where ename='SCOTT' and job='xx';---触发复合索引
  16. select * from emp where ename='SCOTT' or job='xx';---不触发索引
  17. select * from emp where ename='SCOTT';---触发单列索引。

pl/sql

  1. ---pl/sql编程语言
  2. ---pl/sql编程语言是对sql语言的扩展,使得sql语言具有过程化编程的特性。
  3. ---pl/sql编程语言比一般的过程化编程语言,更加灵活高效。
  4. ---pl/sql编程语言主要用来编写存储过程和存储函数等。
  5. ---声明方法
  6. ---赋值操作可以使用:=也可以使用into查询语句赋值
  7. declare
  8. i number(2) := 10;
  9. s varchar2(10) := '小明';
  10. ena emp.ename%type;---引用型变量
  11. emprow emp%rowtype;---记录型变量
  12. begin
  13. dbms_output.put_line(i);
  14. dbms_output.put_line(s);
  15. select ename into ena from emp where empno = 7788;
  16. dbms_output.put_line(ena);
  17. select * into emprow from emp where empno = 7788;
  18. dbms_output.put_line(emprow.ename || '的工作为:' || emprow.job);
  19. end;
  20. ---pl/sql中的if判断
  21. ---输入小于18的数字,输出未成年
  22. ---输入大于18小于40的数字,输出中年人
  23. ---输入大于40的数字,输出老年人
  24. declare
  25. i number(3) := ⅈ
  26. begin
  27. if i<18 then
  28. dbms_output.put_line('未成年');
  29. elsif i<40 then
  30. dbms_output.put_line('中年人');
  31. else
  32. dbms_output.put_line('老年人');
  33. end if;
  34. end;
  35. ---pl/sql中的loop循环
  36. ---用三种方式输出110是个数字
  37. ---while循环
  38. declare
  39. i number(2) := 1;
  40. begin
  41. while i<11 loop
  42. dbms_output.put_line(i);
  43. i := i+1;
  44. end loop;
  45. end;
  46. ---exit循环
  47. declare
  48. i number(2) := 1;
  49. begin
  50. loop
  51. exit when i>10;
  52. dbms_output.put_line(i);
  53. i := i+1;
  54. end loop;
  55. end;
  56. ---for循环
  57. declare
  58. begin
  59. for i in 1..10 loop
  60. dbms_output.put_line(i);
  61. end loop;
  62. end;
  63. ---游标:可以存放多个对象,多行记录。
  64. ---输出emp表中所有员工的姓名
  65. declare
  66. cursor c1 is select * from emp;
  67. emprow emp%rowtype;
  68. begin
  69. open c1;
  70. loop
  71. fetch c1 into emprow;
  72. exit when c1%notfound;
  73. dbms_output.put_line(emprow.ename);
  74. end loop;
  75. close c1;
  76. end;
  77. -----给指定部门员工涨工资
  78. declare
  79. cursor c2(eno emp.deptno%type)
  80. is select empno from emp where deptno = eno;
  81. en emp.empno%type;
  82. begin
  83. open c2(10);
  84. loop
  85. fetch c2 into en;
  86. exit when c2%notfound;
  87. update emp set sal=sal+100 where empno=en;
  88. commit;
  89. end loop;
  90. close c2;
  91. end;
  92. ----查询10号部门员工信息
  93. select * from emp where deptno = 10;

存储过程与存储函数

  1. --存储过程
  2. --存储过程:存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库端
  3. --------可以直接被调用。这一段pl/sql一般都是固定步骤的业务。
  4. ----给指定员工涨100块钱
  5. create or replace procedure p1(eno emp.empno%type)
  6. is
  7. begin
  8. update emp set sal=sal+100 where empno = eno;
  9. commit;
  10. end;
  11. select * from emp where empno = 7788;
  12. ----测试p1
  13. declare
  14. begin
  15. p1(7788);
  16. end;
  17. ----通过存储函数实现计算指定员工的年薪
  18. ----存储过程和存储函数的参数都不能带长度
  19. ----存储函数的返回值类型不能带长度
  20. create or replace function f_yearsal(eno emp.empno%type) return number
  21. is
  22. s number(10);
  23. begin
  24. select sal*12+nvl(comm, 0) into s from emp where empno = eno;
  25. return s;
  26. end;
  27. ----测试f_yearsal
  28. ----存储函数在调用的时候,返回值需要接收。
  29. declare
  30. s number(10);
  31. begin
  32. s := f_yearsal(7788);
  33. dbms_output.put_line(s);
  34. end;
  35. ---out类型参数如何使用
  36. ---使用存储过程来算年薪
  37. create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
  38. is
  39. s number(10);
  40. c emp.comm%type;
  41. begin
  42. select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
  43. yearsal := s+c;
  44. end;
  45. ---测试p_yearsal
  46. declare
  47. yearsal number(10);
  48. begin
  49. p_yearsal(7788, yearsal);
  50. dbms_output.put_line(yearsal);
  51. end;
  52. ----in和out类型参数的区别是什么?
  53. ---凡是涉及到into查询语句赋值或者:=赋值操作的参数,都必须使用out来修饰。
  54. ---存储过程和存储函数的区别
  55. ---语法区别:关键字不一样,
  56. ------------存储函数比存储过程多了两个return
  57. ---本质区别:存储函数有返回值,而存储过程没有返回值。
  58. ----------如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数。
  59. ----------即便是存储过程使用了out类型的参数,起本质也不是真的有了返回值,
  60. ----------而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值。
  61. ----我们可以使用存储函数有返回值的特性,来自定义函数。
  62. ----而存储过程不能用来自定义函数。
  63. ----案例需求:查询出员工姓名,员工所在部门名称。
  64. ----案例准备工作:把scott用户下的dept表复制到当前用户下。
  65. create table dept as select * from scott.dept;
  66. ----使用传统方式来实现案例需求
  67. select e.ename, d.dname
  68. from emp e, dept d
  69. where e.deptno=d.deptno;
  70. ----使用存储函数来实现提供一个部门编号,输出一个部门名称。
  71. create or replace function fdna(dno dept.deptno%type) return dept.dname%type
  72. is
  73. dna dept.dname%type;
  74. begin
  75. select dname into dna from dept where deptno = dno;
  76. return dna;
  77. end;
  78. ---使用fdna存储函数来实现案例需求:查询出员工姓名,员工所在部门名称。
  79. select e.ename, fdna(e.deptno)
  80. from emp e;

ps:call(p1):call只能调用存储过程,不能调用存储函数

触发器

  1. ---触发器,就是制定一个规则,在我们做增删改操作的时候,
  2. ----只要满足该规则,自动触发,无需调用。
  3. ----语句级触发器:不包含有for each row的触发器。
  4. ----行级触发器:包含有for each row的就是行级触发器。
  5. -----------加for each row是为了使用:old或者:new对象或者一行记录。
  6. ---语句级触发器
  7. ----插入一条记录,输出一个新员工入职
  8. create or replace trigger t1
  9. after
  10. insert
  11. on person
  12. declare
  13. begin
  14. dbms_output.put_line('一个新员工入职');
  15. end;
  16. ---触发t1
  17. insert into person values (1, '小红');
  18. commit;
  19. select * from person;
  20. ---行级别触发器
  21. ---不能给员工降薪
  22. ---raise_application_error(-20001~-20999之间, '错误提示信息');
  23. create or replace trigger t2
  24. before
  25. update
  26. on emp
  27. for each row
  28. declare
  29. begin
  30. if :old.sal>:new.sal then
  31. raise_application_error(-20001, '不能给员工降薪');
  32. end if;
  33. end;
  34. ----触发t2
  35. select * from emp where empno = 7788;
  36. update emp set sal=sal-1 where empno = 7788;
  37. commit;
  38. ----触发器实现主键自增。【行级触发器】
  39. ---分析:在用户做插入操作的之前,拿到即将插入的数据,
  40. ------给该数据中的主键列赋值。
  41. create or replace trigger auid
  42. before
  43. insert
  44. on person
  45. for each row
  46. declare
  47. begin
  48. select s_person.nextval into :new.pid from dual;
  49. end;
  50. --查询person表数据
  51. select * from person;
  52. ---使用auid实现主键自增
  53. insert into person (pname) values ('a');
  54. commit;
  55. insert into person values (1, 'b');
  56. commit;

java操作oracle

  1. jar包对应关系

    ----oracle10g ojdbc14.jar
    ----oracle11g ojdbc6.jar

  2. 简单的操作实例

  1. import oracle.jdbc.driver.OracleTypes;
  2. import org.junit.Test;
  3. import java.sql.*;
  4. /**
  5. * Created by SunYuqin in 2018/11/3
  6. * Code without comments is soulless
  7. **/
  8. public class OracleTest {
  9. //简单测试连接
  10. @Test
  11. public void test() throws Exception {
  12. Class.forName("oracle.jdbc.driver.OracleDriver");
  13. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
  14. PreparedStatement pstmt = conn.prepareStatement("select * from person where id=?");
  15. pstmt.setObject(1,1);
  16. ResultSet rs = pstmt.executeQuery();
  17. while (rs.next()){
  18. System.out.println(rs.getString("name"));
  19. }
  20. rs.close();
  21. pstmt.close();
  22. conn.close();
  23. }
  24. //测试存储函数
  25. @Test
  26. public void testfunction() throws Exception {
  27. Class.forName("oracle.jdbc.driver.OracleDriver");
  28. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
  29. CallableStatement pstmt = conn.prepareCall("{?=call f_yearsal(?)}");
  30. pstmt.setObject(2,7788);
  31. pstmt.registerOutParameter(1,OracleTypes.NUMBER);
  32. pstmt.execute();
  33. System.out.println(pstmt.getObject(1));
  34. pstmt.close();
  35. conn.close();
  36. }
  37. //测试存储过程
  38. @Test
  39. public void testprocedure() throws Exception {
  40. Class.forName("oracle.jdbc.driver.OracleDriver");
  41. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.10:1521:orcl", "sun", "root");
  42. CallableStatement pstmt = conn.prepareCall("{call p_yearsal(?, ?)}");
  43. pstmt.setObject(1,7788);
  44. pstmt.registerOutParameter(2, OracleTypes.NUMBER);
  45. pstmt.execute();
  46. System.out.println(pstmt.getObject(2));
  47. pstmt.close();
  48. conn.close();
  49. }
  50. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号