触发器的设计
删除了的数据,只要没有执行commit,使用rollback可以恢复
事务简述
什么是事务
事务是数据库中重要的机制,用于确保数据完整性和并发处理的能力。它将一条/一组SQL语
句当作成一个逻辑上的单元,用于保障这些语句都成功/失败。
事务的特性
事务具有ACID四大特性
A(Atomicity):原子性
C(Consistency):一致性
I(Isolation):隔离性
D(Durability):永久性
直接登录 sqlplus "/ as sysdba"
不允许对方修改 select * from e for update;
update e set sex='女' where eid=1;
commit;
例子1:删除
create or replace trigger del_did
after delete on d
for each row
begin
delete from e where id=:old.id;
end del_did;
/
执行删除
delete from d where id=1;
删除触发器 drop tigger del_id;
例子2:添加
create or replace trigger insert_d
after insert on d
for each row
begin
insert into e(eid,ename,sex,id) values('3','赵3','男','3');
end;
/
执行添加
insert into d values('3','学生');
例子3:
create or replace trigger update_d
after update on d
for each row
begin
update e set id:=new.id where id=:old.id;
end;
/
执行更新
update d set id='4' where id=1;
例子4:设定不能删除raise_application_error
create or replace trigger d_id
after update on d
for each row
begin
if :old.id=1 then
raise_application_error(-20000,'不允许删除!');
end if;
end;
/
例子4:语句型触发器实现操作记录
create table aa(a1 char(100),a2 char(100),a3 char(100));
insert into aa values('aa','123','dd');
create table mylog(curr_user varchar2(100),curr_date date,act char(1));
create or replace trigger dml_aa
after insert or delete or update on aa
begin
if inserting then
insert into mylog values(user,sysdate,'I');
elseif deleting then
insert into mylog values(user,sysdate,'D');
else
insert into mylog values(user,sysdate,'U');
end if;
end;
/
例子5:
create table auto(a char(10),b char(10));
insert into auto values('1','aaa');
create or replace trigger set_no
before insert no auto
for each row
declare
sn number(5);
begin
select myseq.nextval into sn from dual;
:new.a:sn;
end;
/
例子6:替换触发器
create or replace trigger tr_v_e_d
instead of insert on v_emp_dept //v_emp_dept视图
for each row
begin
insert into d values(:new.id,:new.name);
insert into e(eid,ename,sex,id) values(:new.eid,:new.ename,:new.sex;new.id);
end;
/