Oracle 触发器使用

  create tablespace ts1
  datafile 'D:\ts1.dbf'
  size 50m
  autoextend on 
  next 20m maxsize 2048m;
  
  create user student identified by student default tablespace ts1;
  
  grant connect , resource to student;
  grant create view to student;
  create table stuInfo
  (
    stuId int primary key,
    name varchar(10) not null,
    age int check(age>0 and age<100),
    sex varchar(5) default '男',
    telPhone int,
    createTime date default sysdate
  );
  
  create table stuScore
  (
    scoreId int primary key,
    stu_id int references stuInfo(stuId) not null,
    subject varchar(10),
    score float check(score>=0 and score<=100)
  )
  
  
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'张三',20);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'李四',25);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'王五',22);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'赵柳',27);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'钱琪',30);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'啦啦啦',40);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'哒哒哒',30);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'张大',20);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'张二',10);
  insert into stuInfo (stuId,name,age) values (sq_stuInfo.Nextval,'王小二',29);
commit;
select * from stuInfo;
select * from stuScore;

insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,1,'语文',80);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,2,'语文',60);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,3,'语文',85);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,4,'语文',89);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,5,'语文',50);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,6,'语文',100);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,7,'语文',99);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,8,'语文',88);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,9,'语文',74);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,10,'语文',66);
commit;
select * from stuScore;

------------1 before 行级
create or replace trigger tig1
   before insert on stuInfo
   for each row
begin
   if :new.stuid>100 then
   raise_application_error(-20001,'超出范围');    
   end if;
end;       


  insert into stuInfo (stuId,name,age) values (101,'王小二',29);
------------  2 从表删除
  select * from stuInfo;
  create or replace trigger tig3
  before insert or delete or update on stuinfo  
 
  -----  before 语句级
  declare
  -- local variables here
  begin
    if user not in ('STUDENT') then
    raise_application_error(-20001,'没权限修改');
    end if;
  end tig3;
  delete from student.stuInfo where stuid=3;
  
  commit;
----------------  4 instead of
  
  create view view1 as
  select stuInfo.stuid,stuInfo.name,stuScore.subject,stuScore.score from stuInfo inner join stuScore on stuInfo.stuid=stuScore.stu_id;
  
  select * from view1;
  
  create or replace trigger tig4
  instead of delete on view1  
  for each row
  declare
  -- local variables here
  begin
    delete from stuscore where stu_id=:old.stuid;
    delete from stuInfo where stuid =:old.stuid;
    Dbms_Output.put_line('删除成功');
  end tig4;

  delete  from stuInfo where stuid=10;
  select * from stuInfo;
  select *from stuScore;
  
  ---------------after 行级
  create or replace trigger tig7
  after insert or update or delete on stuscore  
  for each row
  declare
  -- local variables here
  begin
    Dbms_Output.put_line('修改数据成功');
  end tig7;     
   
  update stuScore set score = 80 where subject='语文';
  commit;

  
  --------------after 语句级
  
  create or replace trigger tig6
  after insert or update or delete on stuscore  
  
  declare
  
  begin
    Dbms_Output.put_line('数据修改成功');
  end tig6;
  
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,21,'语文',80);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,22,'语文',60);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,23,'语文',85);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,24,'语文',89);
insert into stuScore (scoreId,stu_id,subject,score) values (sq_score.nextval,25,'语文',50);

update stuScore set score = 80 where subject='语文';

commit;
delete from stuScore;
select *from stuScore;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值