您的位置:首页 > 数据库 > Oracle

Oracle第学习四天

2016-02-11 22:53 661 查看
1.光标练习(查询所有员工的姓名和薪水)
    declare
       --定义光标
       cursor cemp is select ename,sal from emp;
       pename emp.ename%type;
        psal emp.sal%type;
        begin
            open cemp;
            loop
                fetch cemp into pname,psal;
                exit when cemp%notfounds;
                --打印
            end loop;
            cloese cemp;
end; 

2.光标的属性:
   %isopen 是否打开
   %rowcount  影响的函数
   %notfound 没有取代记录
   %found

  

3.光标练习2(涨工资)
        declare
            cursor c is select empno,empjob from emp;
            pempno emp.empno%type;
            pjob emp.enpjob%type;
        begin
            open c;
            loop
                fetch c into pempno,pjob;
                exit when cemp%notfound;
                    if pjob=‘’then update 
                        elsif
                    end if;
            end loop;
            close c;
            --  提交,事务的ACID
           commit;
        end;

4.带参数的光标(查询某个部门的员工姓名)
       declare cemp(dno number) is select ename from emp where deptno=dno;
       loop
           open cemp(10);
            ......

5.每次会话,一次性只能打开300个光标
    show parameter cursor  --查看cursor参数,管理员操作 

6.存储在数据库中供所有用户程序调用子程序叫存储过程(不可以使用return)、存储函数(必须使用return)

7.调用存储过程
    a.exec p();
    b.可以在另外一个plsql程序中使用
          begin
            p();
          end;

8.带参数的存储过程,如果是是带参数的话,是输入和输出
    create or replace procedure raise(empno in number)
    as
    --定义变量保存涨前的薪水
    psal emp.sal%type;
    begin
       select  sal  into psal from emp emp where empno=eno;
        update emp set sal=sal+100 where empnp=eno;
        dbms_output.putline();
     end;

9. 查询某个员工的年收入
    create or  replace function a(eno in number)
    return number
    as
    psal emp.sal%type;
    pcomm emo.comm%type;
    begin
        select sal,comm into psal,pcomm from emp where empno=eno;
        return psal*12+nvl(pcomm,0)
    end;
    /

10.原则:如果只有一个返回值,使用存储函数;否则就是用存数过程

11.java调用存储过程:
        String sql="{call queryinfo(?,?,?)}";
       Connection connection=null;
       CallableStatement callableStatement=null;
       try {
           connection=JDBCUtils.getConnection();
        callableStatement=connection.prepareCall(sql);
        //对于int参数
        callableStatement.setInt(1, 7839);
        //对于out参数
        callableStatement.registerOutParameter(2,OracleTypes.VARCHAR);
        callableStatement.registerOutParameter(3, OracleTypes.NUMBER);
        //执行
        callableStatement.execute();
        //取出返回至
        String name=callableStatement.getString(2);
        double sal=callableStatement.getDouble(3);
        System.out.println(name+"--"+sal);
     
                     
  12.在out参数中使用光标,就是返回一个集合(查询所有人员)
     在使用光标,必须要使用包
     --申明包头
     create or replace PACKAGE MYPACKAGE AS 
           type empcursor is ref cursor;
           procedure queryEmpList(dno in number,empList out empcursor);
      END MYPACKAGE;
      --  创建包体

    CREATE OR REPLACE
    PACKAGE BODY MYPACKAGE AS
      procedure queryEmpList(dno in number,empList out empcursor) AS
      BEGIN
          open empList for select * from emp where deptno=dno;
      END queryEmpList;
    END MYPACKAGE;

13.查看包的结构:desc mypackage;

14.java中调用含有包的:
    String sql="{call MYPACKAGE.queryEmpList(?,?)}";
         Connection connection=null;
           CallableStatement callableStatement=null;
           ResultSet rs=null;
           try {
            connection=JDBCUtils.getConnection();
            callableStatement= connection.prepareCall(sql);
            callableStatement.setInt(1, 10);
            callableStatement.registerOutParameter(2,OracleTypes.CURSOR);
            callableStatement.execute();
            rs=((OracleCallableStatement)callableStatement).getCursor(2);
            while(rs.next()){
                String ename=rs.getString("ename");
                System.out.println(ename);
            }
 
15.触发器
    a.语句级触发器:
    b.行级触发器

16.实施复杂的安全行检查(禁止在非工作时间插入员工数据)
     create or replace trigger securityemp
before insert
on emp
begin
  if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
     to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
     --禁止插入
    raise_application_error(-20001,'禁止在非工作时间插入员工数据');
  end if;
end;

17.行级触发器
   create or replace trigger checksal
before update
on emp
for each row
begin
  --if 涨后的薪水 < 涨前的薪水 then
  if :new.sal < :old.sal then
    raise_application_error(-20002,'涨后的工资不能少于涨前的工资.涨前:'||:old.sal||'  涨后:'||:new.sal);
  end if;
end;

18.数据字典:表(包含了权限,用户下面的表)

19.给表加注释:comment on table emp is '这是员工信息'

20.显示注释
    select * from user_tab_comments where table name='EMP'(表名必须大写)
   
                                                            
   
    
                                   2016/2/11  10:50
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: