日期:2014-05-17  浏览次数:20931 次

oracle如何设计触发器,向A表中插入数据时,根据插入的值,向B表插入数据
我创建的两个表
create table Users(
UserName varchar2(8) primary key,
password varchar2(8) not null,
qxno int,
foreign key(qxno) references qx(qxno)
);
create table Student(
StudentName varchar2(8) primary key,
password varchar2(8) not null,
name varchar2(20),
sex varchar2(4),
age int,
qxno int,
foreign key(qxno) references qx(qxno),
constraint chk_age check (age>10 and age<100)
);
我要实现的功能是,在向users表中插入数据时,如果qxno值为3,就向student中插入数据
触发器如下
create or replace trigger insert_users_tea
after insert on users
for each row
when(new.qxno=2)
insert into teacher(teachername,password,qxno)
 values(:new.username,:new.passWord,:new.qxno)
/
插入数据时,提示ORA-04091: 表 ZYUAN.STUDENT 发生了变化, 触发器/函数不能读它
ORA-06512: 在 "ZYUAN.INSERT_USERS_STU", line 1
ORA-04088: 触发器 'ZYUAN.INSERT_USERS_STU' 执行过程中出错
应该怎样写触发器才能实现功能呢?
oracle 触发器 insert

------解决方案--------------------
你的触发器里插入的表怎么是teacher?
还有是在pl/sql的SQL window写的触发器吗?在command window下应该连编译都不能通过
我这写了个触发器,你看下
SQL> set serveroutput on;
SQL> 
SQL> create or replace trigger insert_users_tea
  2    after insert
  3    on users
  4    for each row
  5    begin
  6       if :new.qxno=2 then
  7          insert into Student5(studentname,password,qxno)
  8          values(:new.username,:new.password,:new.qxno);
  9       end if;
 10    end;
 11  /
 
Trigger created