Home Database Mysql Tutorial Oracle触发器(trigger):一般用法

Oracle触发器(trigger):一般用法

Jun 07, 2016 pm 05:30 PM

trigger和procedure,function类似,只不过它不能被显示调用,只能被某个事件触发然后oracle自动去调用.常用的一般是针对一个表或视

trigger和procedure,function类似,只不过它不能被显示调用,只能被某个事件触发然后Oracle自动去调用.常用的一般是针对一个表或视图创建一个trigger,然后对表或视图做某些操作时触发trigger.当然除此之外还有,schema,database级别的trigger.

什么样的操作触发trigger

常见的是DML(insert,update,delete) , DDL(create,alter,drop)语句.

不常见的是schema级别trigger在session连接或断开时触发.database级别trigger在系统启动或退出时触发.

你可能很容易发现如果是select查询操作就没法用到trigger,而有时可能我们想监测谁查看了一些敏感信息,此时只能用到一个叫FGA的东东,可以创建一个审计策略(可以看成加强版的trigger,FGA介绍详见:  ) 

使用触发器注意事项

1.触发器不接受参数,一个表最多可有12个触发器(触发器类型刚好是12种),并且同一时间,同一事件,同一类型的触发器只能有一个(保证触发器操作不冲突嘛).

2.触发器最大为32KB,由于大小受到限制自然也不能使用long,blob这样的大变量.如果实在是有复杂的逻辑,要弄个很复杂的触发器,可以通过procedure或function实现一部分功能,然后调用

3.因为触发器实际上可以看作触发语句的一部分.所以得遵循一些约束条件,比如不能有事务控制语句(commit,rollback,savepoint),DDL语句.为啥子呢,这些特殊语句与一般sql语句的最主要区别是涉及到commit的问题.所以如果触发语句只是一般语句的话自然不能因为trigger的操作带有commit这样的特性了. 

创建触发器

针对表或视图的触发器格式如下: 

CREATE [OR REPLACE] TRIGGER trigger_name

{BEFORE | AFTER }

{INSERT | DELETE | UPDATE [OF column [, column …]]}

[OR {INSERT | DELETE | UPDATE [OF column [, column …]]}...]

ON [schema.]table_name | [schema.]view_name

[REFERENCING {OLD [AS] old | NEW [AS] new| PARENT as parent}]

[FOR EACH ROW ]

[WHEN condition]

PL/SQL_BLOCK | CALL procedure_name;

 

先来看一个简单的,statement级别的trigger怎么创建.假如有表tb1,每insert一点就通过trigger在tblog中记录一些信息.

create or replace trigger tb1_trigger

after insert on tb1

referencing new as new old as old

 

declare

v_info varchar2(100);

begin

v_info := "do a insert";

insert into tblog(info) values(v_info);

end;

 

trigger的创建中有个不太容易理解的内容:一针对row级别的trigger旧值新值问题

row级别trigger旧值新值

针对一个表或视图创建trigger时分为statement级别与row级别的trigger.所谓statement级别是说一个sql语句触发一次trigger,而如果是row级别则一个sql语句涉及到多行数据则trigger会被触发多次.

而旧值就是指要更改的那一行数据在被改之前的值,新值就是用户更新后值.假如表tt只有一列一行数据:88.然后用户执行语句update tt set id = 99 where id = 88;

则旧值指88,新值指99.那你们可能会问用什么方式去得到旧值或新值啊.来举例看下

假如有表tb(eno int); 和表tblog( info varchar2(100)); 假如在tb上创建trigger,tb每update一次则在tblog中记录旧值就更改后的新值.

 

CREATE OR REPLACE TRIGGER tb_trigger

BEFORE UPDATE

ON tb

REFERENCING NEW AS new_val OLD AS old_val --在这里设置名字,然后可引用新值,旧值.如果不指定默认值为new ,old.可以通过:new或:old去引用

FOR EACH ROW

DECLARE

v_info varchar2(100);

BEGIN

 

v_info := 'old value:' ||to_char( :old_val.eno) || 'new value:' || to_char(:new_val.eno);

insert into tblog values(v_info);

END;

条件判断

假如只有在涉及到某一行的操作时触发trigger,假如该触发器是针对updat,delete,insert都触发的情形.咋整呢,自然是多用些when去判断啊.

例如

CREATE OR REPLACE TRIGGER tb_trigger

BEFORE UPDATE or insert or delete

ON tb

REFERENCING NEW AS new_val OLD AS old_val --在这里设置名字,然后可引用新值,旧值

FOR EACH ROW

when (old_val.eno = 99)

DECLARE

v_info varchar2(100);

BEGIN

 

case

when updating then

v_info := 'old value:' ||to_char( :old_val.eno) || 'new value:' || to_char(:new_val.eno);

insert into tblog values(v_info);

 

when inserting then

null;

 

when deleting then

null;

 

end case;

END;

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles