1.创建两个数据表,其字段及其属性完全一致
/*创建表t1*/
create table t1(id varchar2(10) notnull,
uname varchar2(8),
primary key(id));
/*创建表t2*/
create table t2(id varchar2(10) notnull,
unamevarchar2(8),
primary key(id));
2.在两个表中插入相同的几条记录
insert into t1(id,uname)values('11','1111');
insert into t1(id,uname)values('22','2222');
insert into t2(id,uname)values('11','1111');
insert into t2(id,uname)values('22','2222');
3.创建触发器--针对表t1的insert、update以及delete操作,表t2进行相同的操作
create or replace trigger trig_af_t1
after insertor update or delete on t1
for eachrow
declare
l_id
varchar2(10); --临时变量
l_unamevarchar2(8);
begin
if(deletingor updating) then
delete t2
whereid=:old.id;--update和delete命令,删除表t2和表t1中相对应的记录
if(deleting) then
return;
--如果是delete命令,则返回
end if;
endif;
l_id:=:new.id;
--将新的变量值赋给临时变量
l_uname:=:new.uname;
begin
insert into t2(id,uname)
values(l_id,l_uname);
--update和insert命令都将新记录插入到表t2中
end;
end trig_af_t1;