日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

触发器和java的关系_触发器-1 - java ee spring - 博客园

發(fā)布時(shí)間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 触发器和java的关系_触发器-1 - java ee spring - 博客园 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

------------------------------------環(huán)境代碼

create? table student

(stuid varchar2(10) not null,

stuname varchar2(10) not null,

sex char(2)

);

create table subject

(subjectid int,

subjectname varchar2(10)

);

create table score

(

stuid int,

subjectid int,

score int

);

insert into student values (1001, 'wind', '男');

insert into student values (1002, 'snow', '女');

insert into student values (1003, 'apple', '男');

insert into subject values (1, 'oracle');

insert into subject values (2, 'java');

insert into score values (1001, 1, 90);

insert into score values (1002, 2, 88);

create table apple

(sid int,

sno int);

---------------主體部分

一、觸發(fā)器

1.觸發(fā)器具有三個(gè)部分

(1).觸發(fā)事件

(2).可選的觸發(fā)器約束條件

(3).觸發(fā)器動(dòng)作

2.可以新建對(duì)應(yīng)如下語句的觸發(fā)器

(1).DML語句(insert、delete、update)

(2)DDL語句(create、alter、drop)

(3).數(shù)據(jù)庫(kù)操作(servererror、logon、logoff、startup、shutdown)

3.可以創(chuàng)建觸發(fā)器的對(duì)象:1.數(shù)據(jù)庫(kù)表 2.數(shù)據(jù)庫(kù)視圖 3.用戶模式 4.數(shù)據(jù)庫(kù)實(shí)例

4.觸發(fā)器類型

(1).DML觸發(fā)器(包括行級(jí)觸發(fā)器、語句級(jí)觸發(fā)器)

(2).系統(tǒng)觸發(fā)器

(3).替代觸發(fā)器

(4).模式觸發(fā)器

5.執(zhí)行DML語句的順序

(1).執(zhí)行before語句級(jí)觸發(fā)器(如果存在)

(2).對(duì)于受語句影響的每一行執(zhí)行DML語句

(3).執(zhí)行after語句觸發(fā)器(如果存在)

6.兩個(gè)特殊的值

:new 新產(chǎn)生的值

:old 原是的值

7.觸發(fā)器中的謂語

(1). inserting

(2).updating

(3). deleting

---------------------------------------------------------------

****************************************************************

一、語句觸發(fā)器

****************************************************************

--------------------------------------------------------------

一、語句觸發(fā)器

create or replace trigger schemaname.triggername

before | after |delete |update of 列名

on 表名

[for each row]

when 條件

------------------------------------------------------

第一部分:before觸發(fā)器

------------------------------------------------------

--案例01:新建一個(gè)測(cè)試的行前觸發(fā)器

create or replace trigger tr01

before insert on student

begin

dbms_output.put_line('這個(gè)是行前觸發(fā)器!');

end;

--案例02:禁止工作人員在非工作日修改表信息

create table k01

(sid int,

sno int);

create or replace trigger tr101

before insert or update or delete on k01

begin

if to_char(sysdate, 'DY','nls_date_language=AMERICAN')

in ('SAT','SUN') then

raise_application_error(-20001,'不能休息日修改該表數(shù)據(jù)');

end if;

end;

--案例03:使用三個(gè)條件謂語

create or replace trigger tr102

before insert or update or delete on k01

begin

if to_char(sysdate, 'DY','nls_date_language=AMERICAN')

in ('SAT','SUN') then

case

when inserting then

begin

raise_application_error(-20001,'對(duì)不起,不能完成插入操作!');

end;

when updating then

begin

raise_application_error(-20002,'對(duì)不起,不能在休息日更新該表數(shù)據(jù)!');

end;

when deleting? then

begin

raise_application_error(-20003,'對(duì)不起,不能在休息日刪除該表數(shù)據(jù)!');

end;

end case;

end if;

end;

------------------------------------------------------

第二部分:after觸發(fā)器

------------------------------------------------------

--案例01:新建一個(gè)測(cè)試的行后觸發(fā)器

create or replace trigger tr02

after update on student? /*沒有for each row說明是個(gè)表級(jí)別觸發(fā)器*/

begin

dbms_output.put_line('這個(gè)是表級(jí)update觸發(fā)器!');

/*如果你一次更新的語句是幾條只觸發(fā)一次!*/

end;

--案例02:

/*新建一個(gè)觸發(fā)器完成審計(jì)工作,審計(jì)在k01表上的insert、update、delete的操作次數(shù)、最早執(zhí)行時(shí)間、最近執(zhí)行時(shí)間*/

第一步:新建用于記錄審計(jì)信息的審計(jì)表

create table audit_table

(

name varchar2(10),

login_user varchar2(20),

ins int,

upd int,

del int,

starttime date,

endtime date

);

第二步:新建觸發(fā)器

方法1:用于后臺(tái)監(jiān)控

create or replace trigger tr_audit_k01_01

after insert or update or delete on k01

declare

cnt int;

begin

select count(*) into cnt from audit_table where name='K01';

if cnt=0 then

insert into audit_table values ('K01',ora_login_user,0,0,0,sysdate, null);

end if;

case

when inserting then

update audit_table set ins=ins+1, endtime=sysdate where name='K01';

when updating then

update audit_table set upd=upd+1, endtime=sysdate where name='K01';

when deleting then

update audit_table set del=del+1, endtime=sysdate where name='K01';

end case;

end;

方法2:前后臺(tái)監(jiān)控

create or replace trigger tr_audit_k01_02

after insert or update or delete on k01

declare

cnt int;

begin

select count(*) into cnt from audit_table where name='K01';

if cnt=0 then

insert into audit_table values ('K01',ora_login_user,0,0,0,sysdate, null);

end if;

case

when inserting then

begin

update audit_table set ins=ins+1, endtime=sysdate where name='K01';

dbms_output.put_line('你完成了數(shù)據(jù)插入操作!!');

end;

when updating then

begin

update audit_table set upd=upd+1, endtime=sysdate where name='K01';

dbms_output.put_line('你完成了數(shù)據(jù)更新操作!');

end;

when deleting then

begin

update audit_table set del=del+1, endtime=sysdate where name='K01';

dbms_output.put_line('你在進(jìn)行刪除操作,請(qǐng)慎重!!');

end;

end case;

end;

總結(jié)

以上是生活随笔為你收集整理的触发器和java的关系_触发器-1 - java ee spring - 博客园的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。