Oracle存储过程、触发器、分页以及数据库的管理与备份

1、分页

思路:orcale 要使用分页得使用rownum (行号来进行分页),rownum 不支持大于号,查询rownum 起别名

 《多少    》多少


2、数据库的备份与管理


用户的创建与授权:


创建用户
create user username identified by password
修改密码
alert user username identified by password;
用户授权
grant dba to username;
锁定用户  |  解锁
alter user username  account  lock |unlock


创建连接
conn username/password


授予权限:
    默认的普通用户scott默认未解锁,不能进行那个使用,新建的用户也没有任何权限,必须授予权限
    多个权限用,逗号隔开

    grant create session to zhangsan;//授予zhangsan用户创建session的权限,即登陆权限

  grant unlimited tablespace to zhangsan;//授予zhangsan用户使用表空间的权限

  grant create table to zhangsan;//授予创建表的权限

  grante drop table to zhangsan;//授予删除表的权限

  grant insert table to zhangsan;//插入表的权限

  grant update table to zhangsan;//修改表的权限

  grant all to public;//这条比较重要,授予所有权限(all)给所有用户(public)


用户表与表之间的授权:


     oralce对权限管理比较严谨,普通用户之间也是默认不能互相访问的,需要互相授权

  grant select on tablename to zhangsan;//授予zhangsan用户查看指定表的权限

  grant drop on tablename to zhangsan;//授予删除表的权限

  grant insert on tablename to zhangsan;//授予插入的权限

  grant update on tablename to zhangsan;//授予修改表的权限

  grant insert(id) on tablename to zhangsan;

  grant update(id) on tablename to zhangsan;//授予对指定表特定字段的插入和修改权限,注意,只能是insert和update

  grant alert all table to zhangsan;//授予zhangsan用户alert任意表的权限



撤销权限:

    基本语法同grant,关键字为revoke

revokeselect on tablename to zhangsan;//授予zhangsan用户查看指定表的权限

。。。。。。。同上


查看权限:

     select * from user_sys_privs;//查看当前用户所有权限

  select * from user_tab_privs;//查看所用用户对表的权限



角色:

  角色即权限的集合,可以把一个角色授予给用户

  create role myrole;//创建角色

  grant create session to myrole;//将创建session的权限授予myrole

  grant myrole to zhangsan;//授予zhangsan用户myrole的角色

  drop role myrole;删除角色





3、触发器(trigger)


语句级触发器
在指定的操作语句操作之前或者之后执行一次,不管这条语句影响了多少行
行级触发器 触发语句作用的每一条记录都被触发,在行级触发器中使用old new 伪记录变量,识别值的状态
or  |   on 
字段   | 表


语句触发器


create or replace trigger  demo2
 before insert 
  on emp 
  declare
  begin 
    if to_number(to_char(sysdate,'hh24')) not between 9 and 17 or to_char(sysdate,'day') in ('星期六','星期日','星期三')
      then RAISE_APPLICATION_ERROR(-20001, '禁止在非工作日时间插入');
      end if;
  end;


行级触发器

 


4、存储过程


存储过程和存储函数(不用declare  用 as  ) 概念:指存储在数据库中供所有用户程序调用的字程序叫做存储过程、存储函数 

区别(什么时候用):如果只有一个返回值用存储函数,如果有多个返回值,就用存储过程(申明包结构,创建包体,:游标)







存储过程创建方式:
返回多个值:包结构

create or replace procedure demo1(mempno number,psal number)
  as 
    msal emp.sal%type;
begin 
    select sal into msal from emp where empno = mempno;

    update emp set sal =sal+psal;

    dbms_output.put_line('涨前:'||msal||'   涨后:'||(msal+psal));
    commit;
end;







存储函数的创建方式:
(返回单个值)
create or replace function queryempincome(eno in number)
return number--返回一个值
as
   --定义变量保存月薪和奖金
   psal emp.sal%type;
   pcomm emp.comm%type;
begin
   select sal,comm into psal,pcomm from emp where empno=eno;

   --返回年收入
   return psal*12+nvl(pcomm,0);
end;

    存储过程返回多个值,将游标以集合的方式返回:    需要申明包结构、创建包体




> 原文链接 |
posted @ 2024-06-25 10:34  汉源魂  阅读(44)  评论(0)    收藏  举报