触发器和java的关系_触发器-1 - java ee spring - 博客园
------------------------------------環(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ā)器具有三個部分
(1).觸發(fā)事件
(2).可選的觸發(fā)器約束條件
(3).觸發(fā)器動作
2.可以新建對應(yīng)如下語句的觸發(fā)器
(1).DML語句(insert、delete、update)
(2)DDL語句(create、alter、drop)
(3).數(shù)據(jù)庫操作(servererror、logon、logoff、startup、shutdown)
3.可以創(chuàng)建觸發(fā)器的對象:1.數(shù)據(jù)庫表 2.數(shù)據(jù)庫視圖 3.用戶模式 4.數(shù)據(jù)庫實例
4.觸發(fā)器類型
(1).DML觸發(fā)器(包括行級觸發(fā)器、語句級觸發(fā)器)
(2).系統(tǒng)觸發(fā)器
(3).替代觸發(fā)器
(4).模式觸發(fā)器
5.執(zhí)行DML語句的順序
(1).執(zhí)行before語句級觸發(fā)器(如果存在)
(2).對于受語句影響的每一行執(zhí)行DML語句
(3).執(zhí)行after語句觸發(fā)器(如果存在)
6.兩個特殊的值
: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:新建一個測試的行前觸發(fā)器
create or replace trigger tr01
before insert on student
begin
dbms_output.put_line('這個是行前觸發(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:使用三個條件謂語
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,'對不起,不能完成插入操作!');
end;
when updating then
begin
raise_application_error(-20002,'對不起,不能在休息日更新該表數(shù)據(jù)!');
end;
when deleting? then
begin
raise_application_error(-20003,'對不起,不能在休息日刪除該表數(shù)據(jù)!');
end;
end case;
end if;
end;
------------------------------------------------------
第二部分:after觸發(fā)器
------------------------------------------------------
--案例01:新建一個測試的行后觸發(fā)器
create or replace trigger tr02
after update on student? /*沒有for each row說明是個表級別觸發(fā)器*/
begin
dbms_output.put_line('這個是表級update觸發(fā)器!');
/*如果你一次更新的語句是幾條只觸發(fā)一次!*/
end;
--案例02:
/*新建一個觸發(fā)器完成審計工作,審計在k01表上的insert、update、delete的操作次數(shù)、最早執(zhí)行時間、最近執(zhí)行時間*/
第一步:新建用于記錄審計信息的審計表
create table audit_table
(
name varchar2(10),
login_user varchar2(20),
ins int,
upd int,
del int,
starttime date,
endtime date
);
第二步:新建觸發(fā)器
方法1:用于后臺監(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:前后臺監(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('你在進行刪除操作,請慎重!!');
end;
end case;
end;
總結(jié)
以上是生活随笔為你收集整理的触发器和java的关系_触发器-1 - java ee spring - 博客园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java单链表例子_写一个java链表的
- 下一篇: jfinal js 拦截_jfinal