SQL触发器插入

时间:2018-06-28 17:09:26

标签: sql oracle triggers

当将新的BOLETA插入数据库时​​,我需要在COMISION_VENTA表中插入NRO_BOLETA,MONTO_BOLETA * 0,15(表COMISION_VENTA中的VALOR_COMISION)值

BOLETA表:

CREATE TABLE BOLETA 
(
      nro_boleta NUMBER(10) CONSTRAINT PK_BOLETA PRIMARY KEY, 
      fecha_boleta DATE  NOT NULL, 
      monto_boleta NUMBER(15)  NOT NULL, 
      id_cliente NUMBER(10) NOT NULL,
      numrut_emp NUMBER(10) NOT NULL,

      CONSTRAINT FK_FACTURA_VENDEDOR 
          FOREIGN KEY(numrut_emp) REFERENCES empleado(numrut_emp)
); 

COMISION_VENTA表:

CREATE TABLE COMISION_VENTA
(
      nro_boleta NUMBER(10) NOT NULL CONSTRAINT PK_COMISION_VENTA PRIMARY KEY ,
      valor_comision  NUMBER(10) NOT NULL,

      CONSTRAINT FK_COMISION_VENTA_BOLETA 
          FOREIGN KEY(nro_boleta) REFERENCES BOLETA(nro_boleta)
);

触发:

CREATE OR REPLACE TRIGGER asignacion
BEFORE INSERT OR DELETE OR UPDATE ON BOLETA
FOR EACH ROW
BEGIN
    IF INSERTING THEN
        INSERT INTO COMISION_VENTA(NRO_BOLETA, VALOR_COMISION) 
        VALUES(:NEW.NRO_BOLETA, ROUND(:NEW.MONTO_BOLETA*0,15));
    END IF;
END;

插入和错误:

INSERT INTO BOLETA(NRO_BOLETA, FECHA_BOLETA, MONTO_BOLETA, ID_CLIENTE, NUMRUT_EMP)
VALUES (28, '26/06/18', 258999, 3000, 12456905)
  

通知错误
  ORA-02291:违反完整性约束(ADMIN_ENS.FK_COMISION_VENTA_BOLETA)-找不到父键
  ORA-06512:位于“ ADMIN_ENS.ASIGNACION”的第3行,
  ORA-04088:执行触发器'ADMIN_ENS.ASIGNACION'时发生错误

1 个答案:

答案 0 :(得分:0)

将触发代码更新为AFTER TRIGGER-

CREATE OR REPLACE TRIGGER asignacion
**after** INSERT OR DELETE OR UPDATE ON BOLETA
FOR EACH ROW
BEGIN
    IF INSERTING THEN
        INSERT INTO COMISION_VENTA(NRO_BOLETA, VALOR_COMISION) 
        VALUES(:NEW.NRO_BOLETA, ROUND(:NEW.MONTO_BOLETA*0,15));
    END IF;
END;
/

这应该有效。