Oracle触发器详细 和 Oracle 创建序列号

摘要:
此例表示如果列department_id不等于80的时候,触发器就会执行。比如,无论update多少行,也只会调用一次update语句触发器。Createtablefoo;Createtriggerbiud_fooBeforeinsertorupdateordeleteOnfooBeginIfusernotinthenRaise_application_error;Endif;End;/即使SYS,SYSTEM用户也不能修改foo表[试验]对修改表的时间、人物进行日志记录。Createorreplacetriggerbiud_employee_copyBeforeinsertorupdateordeleteOnemployees_copyBeginInsertintoemployees_logValues;End;/4、测试updateemployees_copysetsalary=salary*1.1;select*fromemployess_log;5、确定是哪个语句起作用?

由于这段时间要导入旧的数据库数据到新的数据库中,然而旧的数据库和新的数据库有些表的设计或字段不一样,

需要用到触发器,比如新的数据库a表有aid的主键字段(或自动递增),而旧的数据库a表没有aid的主键字段,

这时候要通过调用触发器,在copy数据时自动插入aid值同时要保证主键唯一性。

解决方案:

在旧数据库新建一个视图,通过视图导入到新的数据库中

--PRODUCT_KIND_INFO视图
create or replace view Z_PRODUCT_KIND_INFO as
select *
from PRODUCT_KIND_INFO;
--#copy数据脚本
insert into PRODUCT_KIND_INFO select * from iptv_hn_new53.Z_PRODUCT_KIND_INFO;

注意:

相同的数据库服务器,不同的用户名之间调用视图 “用户名.视图名”;

不同的服务器之间调用要建立 “Database links” ,然后使用 “视图名@gd_tmp” 调用。

-- Drop existing database link
drop public database link GD_TMP;
-- Create database link
create public database link GD_TMP
connect to GD_TMP
using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = mytestdb)
)
)';

在PL/SQL里面显示的图片:

Oracle触发器详细 和 Oracle 创建序列号第1张

insert into price_plan select * from v_price_plan@gd_tmp

oracle学习笔记_触发器

触发器

是特定事件出现的时候,自动执行的代码块。类似于存储过程,但是用户不能直接调用他们。

功能:
1、 允许/限制对表的修改
2、 自动生成派生列,比如自增字段
3、 强制数据一致性
4、 提供审计和日志记录
5、 防止无效的事务处理
6、 启用复杂的业务逻辑

开始
create trigger biufer_employees_department_id
before insert or update
of department_id
on employees
referencing old as old_value
new as new_value
for each row
when (new_value.department_id<>80 )
begin
:new_value.commission_pct :=0;
end;
/

触发器的组成部分:
1、 触发器名称
2、 触发语句
3、 触发器限制
4、 触发操作

1、 触发器名称
create trigger biufer_employees_department_id
命名习惯:
biufer(before insert update for each row)
employees 表名
department_id 列名

2、 触发语句
比如:
表或视图上的DML语句
DDL语句
数据库关闭或启动,startup shutdown 等等
before insert or update
of department_id
on employees
referencing old as old_value
new as new_value
for each row

说明:
1、 无论是否规定了department_id ,对employees表进行insert的时候
2、 对employees表的department_id列进行update的时候

3、 触发器限制
when (new_value.department_id<>80 )

限制不是必须的。此例表示如果列department_id不等于80的时候,触发器就会执行。
其中的new_value是代表跟新之后的值。

4、 触发操作
是触发器的主体
begin
:new_value.commission_pct :=0;
end;

主体很简单,就是将更新后的commission_pct列置为0

触发:
insert into employees(employee_id,
last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values( 12345,’Chen’,’Donny’, sysdate, 12,
‘donny@hotmail.com’,60,10000,.25);

select commission_pct from employees where employee_id=12345;

触发器不会通知用户,便改变了用户的输入值。

触发器类型:
1、 语句触发器
2、 行触发器
3、 INSTEAD OF 触发器
4、 系统条件触发器
5、 用户事件触发器

1、 语句触发器
是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERT、UPDATE、

DELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次

。比如,无论update多少行,也只会调用一次update语句触发器。

例子:
需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。
Create table foo(a number);

Create trigger biud_foo
Before insert or update or delete
On foo
Begin
If user not in (‘DONNY’) then
Raise_application_error(-20001, ‘You don’t have access to modify this table.’);
End if;
End;
/

即使SYS,SYSTEM用户也不能修改foo表

[试验]
对修改表的时间、人物进行日志记录。

1、 建立试验表
create table employees_copy as select *from hr.employees

2、 建立日志表
create table employees_log(
who varchar2(30),
when date);

3、 在employees_copy表上建立语句触发器,在触发器中填充employees_log 表。
Create or replace trigger biud_employee_copy
Before insert or update or delete
On employees_copy
Begin
Insert into employees_log(
Who,when)
Values( user, sysdate);
End;
/
4、 测试
update employees_copy set salary= salary*1.1;

select *from employess_log;

5、 确定是哪个语句起作用?
即是INSERT/UPDATE/DELETE中的哪一个触发了触发器?
可以在触发器中使用INSERTING / UPDATING / DELETING 条件谓词,作判断:
begin
if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;
end;

if updating(‘COL1’) or updating(‘COL2’) then
------
end if;

[试验]
1、 修改日志表
alter table employees_log
add (action varchar2(20));

2、 修改触发器,以便记录语句类型。
Create or replace trigger biud_employee_copy
Before insert or update or delete
On employees_copy
Declare
L_action employees_log.action%type;
Begin
if inserting then
l_action:=’Insert’;
elsif updating then
l_action:=’Update’;
elsif deleting then
l_action:=’Delete’;
else
raise_application_error(-20001,’You should never ever get this error.’);

Insert into employees_log(
Who,action,when)
Values( user, l_action,sysdate);
End;
/

3、 测试
insert into employees_copy( employee_id, last_name, email, hire_date, job_id)
values(12345,’Chen’,’Donny@hotmail’,sysdate,12);

select *from employees_log

update employees_copy set salary=50000 where employee_id = 12345;

2、 行触发器
是指为受到影响的各个行激活的触发器,定义与语句触发器类似,有以下两个例外:
1、 定义语句中包含FOR EACH ROW子句
2、 在BEFORE……FOR EACH ROW触发器中,用户可以引用受到影响的行值。
比如:

定义:
create trigger biufer_employees_department_id
before insert or update
of department_id
on employees_copy
referencing old as old_value
new as new_value
for each row
when (new_value.department_id<>80 )
begin
:new_value.commission_pct :=0;
end;
/

Referencing 子句:
执行DML语句之前的值的默认名称是 :old ,之后的值是 :new
insert 操作只有:new
delete 操作只有 :old
update 操作两者都有

referencing子句只是将new 和old重命名为new_value和old_value,目的是避免混淆。比如操作一个名为

new的表时。
作用不很大。

[试验]:为主健生成自增序列号

drop table foo;
create table foo(id number, data varchar2(20));
create sequence foo_seq;

create or replace trigger bifer_foo_id_pk
before insert on foo
for each row
begin
select foo_seq.nextval into :new.id from dual;
end;
/

insert into foo(data) values(‘donny’);
insert into foo values(5,’Chen’);
select * from foo;

3、 INSTEAD OF 触发器更新视图

Create or replace view company_phone_book as
Select first_name||’, ’||last_name name, email, phone_number,
employee_id emp_id
From hr.employees;

尝试更新email和name
update hr.company_phone_book
set name=’Chen1, Donny1’
where emp_id=100

create or replace trigger update_name_company_phone_book
INSTEAD OF
Update on hr.company_phone_book
Begin
Update hr.employees
Set employee_id=:new.emp_id,
First_name=substr(:new.name, instr(:new.name,’,’)+2),
last_name= substr(:new.name,1,instr(:new.name,’,’)-1),
phone_number=:new.phone_number,
email=:new.email
where employee_id=:old.emp_id;
end;

4、 系统事件触发器
系统事件:数据库启动、关闭,服务器错误

create trigger ad_startup
after startup
on database
begin
-- do some stuff
end;
/

5、 用户事件触发器
用户事件:用户登陆、注销,CREATE / ALTER / DROP / ANALYZE / AUDIT / GRANT / REVOKE /

RENAME / TRUNCATE / LOGOFF

例子:记录删除对象

1. 日志表
create table droped_objects(
object_name varchar2(30),
object_type varchar2(30),
dropped_on date);

2.触发器
create or replace trigger log_drop_trigger
before drop on donny.schema
begin
insert into droped_objects values(
ora_dict_obj_name, -- 与触发器相关的函数
ora_dict_obj_type,
sysdate);
end;
/

3. 测试
create table drop_me(a number);
create view drop_me_view as select *from drop_me;
drop view drop_me_view;
drop table drop_me;

select *from droped_objects

禁用和启用触发器
alter trigger <trigger_name> disable;
alter trigger <trigger_name> enable;

事务处理:
在触发器中,不能使用commit / rollback
因为ddl语句具有隐式的commit,所以也不允许使用

视图:
dba_triggers

原文链接:http://www.souzz.net/html/database/ORACLE/20428.html

Oracle 创建序列号

1. 创建序列号里各参数的解释

SQL> CREATE SEQUENCE name [INCREMENT BY n]

[START WITH n] [{MAXVALUE n | NOMAXVALUE}]

[{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}]

[{CACHE n | NOCACHE}]

INCREMENT BY n 一次增长n 个数字

NOMAXVALUE 缺省值10E+27

NOMINVALUE 缺省值1

NOCYCLE 不循环, 常用于唯一关键字

CACHE n 在内存里缓存n个序列,出错回退时会丢失

oracle8i里默认的n是20

序列号的名称一般可以采用“表名_字段名”的命名规则

2. 插入自动增长序列号字段的方法

INSERT时如果要用到从1开始自动增长的数字做唯一关键字, 应该先建立一个序列号.

CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1

MAXVALUE 99999 NOCYCLE NOCACHE;

其中最大的值按字段的长度来定,比如定义的自动增长的序列NUMBER , 最大值为999999

INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL

例子: SQL> insert into s_dept(id, name, region_id) values (s_dept_id.nextval, 'finance', 2);

1 row created.

只有运行了序列号的名称. nextval后序列号的名称. currval 才有效才有值.

3. 查询序列号的情况

SQL> select sequence_name, min_value, max_value, increment_by, last_number from user_sequences;

其中last_number指的是序列号的下一个值.

4. 改变序列号

SQL> ALTER SEQUENCE sequence [INCREMENT BY n] [{MAXVALUE n | NOMAXVALUE}]

[{MINVALUE n | NOMINVALUE}]

[{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];

注意: 不能改变它的起始值

如果要改变序列的起始值, 先把序列号删除掉, 再新建一个.

5. 删除序列号

SQL>DROP SEQUENCE sequence;

6. 不能用序列号的nextval和currval的地方

视图的查询

有distinct的查询

有group by,having,order by的查询

有子查询的查询

表里的缺省值

版权声明:本文为博主原创文章,未经博主允许不得转载。

免责声明:文章转载自《Oracle触发器详细 和 Oracle 创建序列号》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[vue/no-parsing-error] Parsing error: invalid-first-character-of-tag-name.eslint-plugin-vue Parsing error: invalid-first-character-of-tag-name.eslint(vue/no-parsing-error)flink连接hbase方法及遇到的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

从Oracle转到Mysql前需了解的50件事

我本人比较关心的几点:   1. 对子查询的优化表现不佳.   2. 对复杂查询的处理较弱   4. 性能优化工具与度量信息不足   12. 支持 SMP (对称多处理器),但是如果每个处理器超过 4 或 8 个核(core)时,Mysql 的扩展性表现较差.   15. 没有基于回滚(roll-back)的恢复功能,只有前滚(roll-forward)的...

【.NET-MVC】ASP.NET MVC学习笔记-概述

第 1 篇:理解控制器和视图 MVC概述 MVC理论就是模型、视图、控制器。   (其实也是种思想,为了让前端、程序、数据分开,也是想解耦) MVC请求流程是:访问控制器,控制器来创建模型,模型去数据库获取数据,模型再返回给视图。 VS 2013 创建MVC4 Web应用程序,自动生成MVC框架对应的文件夹,分别是Model、View、Controlle...

Windows,Linux的select函数功能差异

Windows,Linux的select函数功能差异 感谢主,Windows当年也实现了select函数,这让我们的跨平台大业至少顺畅了一节。但由于Windows渗入骨髓的叛逆心理,他总要和UNIX的实现保持一些差别,让你无可奈何。首先是Windows的select函数的参数接口设计和Linux下有较大差别,这个在我的《设计极其糟糕的select函数》就讨...

如何导出远程oracle数据库中的表结构

从远程oracle数据库上导出指定表的表结构语句有两种方法: 方法一:通过sql语句获得 1,make sure that you can connect the remote database. 2,enter into the sqlplus,and execute the command: select dbms_metadata.getddl('T...

Spring Boot页面中select选项绑定数据库数据

  在一个select框中,option往往是写好的,这样难以适应数据库中项目的动态变化,因此需要将option中的项目与数据库数据进行绑定,本项目使用Spring Boot进行开发,下面演示绑定方法。   首先在前端定义一个基本select框,在这里把第一项写好了,并显示为select框的默认项。 <select id="selectshijuan...

oracle中Blob和Clob类型的区别

一、oracle中Blob和Clob类型的区别BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的。其实两个是可以互换的的,或者可以直接用LOB字段代替这两个。但是为了更好的管理ORACLE数据库,通常像图片、文件、音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去。而像文章或者是较长的文字,就用CLOB...