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