ERROR 1442 - Trigger AFTER INSERT - MySql

0

My database has the following structure:

CREATE TABLE BAIRRO
(
  BAI_COD INT(4) AUTO_INCREMENT NOT NULL,
  BAI_NOME VARCHAR(200)NOT NULL,
  CONSTRAINT PK_BAIRRO PRIMARY KEY(BAI_COD)
);

CREATE TABLE PAGAMENTO
(
    PAG_COD INT(2) AUTO_INCREMENT NOT NULL,
    PAG_DESCRICAO VARCHAR(50)NOT NULL UNIQUE,
    CONSTRAINT PK_PAGAMENTO PRIMARY KEY(PAG_COD)
);

CREATE TABLE FABRICANTE
(
    FAB_CNPJ VARCHAR(14)NOT NULL,
    FAB_NOME VARCHAR(200)NOT NULL UNIQUE,
    CONSTRAINT PK_FABRICANTE PRIMARY KEY(FAB_CNPJ)
);

CREATE TABLE MODELO
(
    MOD_COD INT(5) AUTO_INCREMENT NOT NULL,
    MOD_NOME VARCHAR(200) UNIQUE,
    MOD_FABRICANTE VARCHAR(14)NOT NULL,
    CONSTRAINT PK_MODELO PRIMARY KEY(MOD_COD),
    CONSTRAINT FK_FABRICANTE FOREIGN KEY(MOD_FABRICANTE)REFERENCES FABRICANTE(FAB_CNPJ)
    ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE CARRO
(
    CAR_PLACA VARCHAR(10)NOT NULL,
    CAR_COR VARCHAR(40)NOT NULL,
    CAR_ANO INT(4) NOT NULL,
    CAR_STATUS VARCHAR(12)NOT NULL DEFAULT 'DISPONÍVEL',
    CAR_VALOR DECIMAL(10,2) NOT NULL ,
    CAR_MODELO INT(5) NOT NULL,
    CONSTRAINT PK_CARRO PRIMARY KEY(CAR_PLACA),
    CONSTRAINT FK_MODELO FOREIGN KEY(CAR_MODELO)REFERENCES MODELO(MOD_COD)
    ON DELETE CASCADE ON UPDATE CASCADE
);


CREATE TABLE USUARIO
(
    USU_COD INT(5) AUTO_INCREMENT NOT NULL,
    USU_NOME VARCHAR(200)NOT NULL,
    USU_LOGIN VARCHAR(20)NOT NULL UNIQUE,
    USU_SENHA VARCHAR(20)NOT NULL,
    CONSTRAINT PK_USUARIO PRIMARY KEY(USU_COD)
);


CREATE TABLE CLIENTE
(
    CLI_CNH VARCHAR(10)NOT NULL,
    CLI_NOME VARCHAR(200)NOT NULL,
    CLI_FONE VARCHAR(11)NOT NULL,
    CLI_BAIRRO INT(4) NOT NULL,
    CONSTRAINT PK_CLIENTE PRIMARY KEY(CLI_CNH),   
    CONSTRAINT FK_BAIRRO FOREIGN KEY(CLI_BAIRRO)REFERENCES BAIRRO(BAI_COD)
    ON DELETE CASCADE ON UPDATE CASCADE
);


CREATE TABLE LOCACAO
(
    LOC_COD INT(5) AUTO_INCREMENT NOT NULL,
    DTALUGUEL DATE NOT NULL,
    DTDEVOLUCAO DATE CHECK (DTDEVOLUCAO>=DTALUGUEL),
    VALORALOC DECIMAL(10,2),
    STATUS VARCHAR(7)NOT NULL DEFAULT 'ABERTO',
    QTDE_DIAS INT(5) NOT NULL,
    LOC_CLIENTE VARCHAR(10)NOT NULL,
    LOC_CARRO VARCHAR(10)NOT NULL,
    LOC_USUARIO INT(5) NOT NULL,
    LOC_PAGAMENTO INT(2) NOT NULL,
    CONSTRAINT PK_LOCACAO PRIMARY KEY(LOC_COD),
    CONSTRAINT FK_CLIENTE FOREIGN KEY(LOC_CLIENTE)REFERENCES CLIENTE(CLI_CNH)
    ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FK_CARRO FOREIGN KEY(LOC_CARRO)REFERENCES CARRO(CAR_PLACA)
    ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FK_USUARIO FOREIGN KEY(LOC_USUARIO)REFERENCES USUARIO(USU_COD)
    ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FK_PAGAMENTO FOREIGN KEY(LOC_PAGAMENTO)REFERENCES PAGAMENTO(PAG_COD)
    ON DELETE CASCADE ON UPDATE CASCADE
);

-- Insert que podem ser úteis para o teste

INSERT INTO fabricante(fab_cnpj, fab_nome) VALUES ('59275792000150', 'Chevrolet');

INSERT INTO modelo(mod_nome, mod_fabricante)VALUES ('Prisma Joy', '59275792000150');

INSERT INTO carro(car_placa, car_cor, car_ano, car_valor, car_modelo)
    VALUES ('HHKU1555', 'Prata', 2013, 1711.0, 1);

INSERT INTO cliente(cli_cnh, cli_nome, cli_bairro, cli_fone)
    VALUES ('5253696855', 'Otávio Rodrigues', 13, '92982454750');

INSERT INTO PAGAMENTO (PAG_DESCRICAO) VALUES ('Dinheiro');

INSERT INTO USUARIO(USU_NOME, USU_LOGIN, USU_SENHA)VALUES('MARIO BRÓS', 'MARIO', '123');  

INSERT INTO locacao(dtaluguel, qtde_dias, loc_cliente, loc_carro, loc_usuario, loc_pagamento)
    VALUES ('2018-02-21', 10, '5222222855','KIUS1290', 1, 1);

My idea is as follows after attaching a CAR in the LOCATION the value of your daily value in the CAR_VALOR column of the CAR table must be copied to the VALORALOC column of the LOCATION table, and also, after that CAR is linked in that LOCATION, your status that is represented by the CAR_STATUS column of the CAR table should be updated to 'UNAVAILABLE'.

For this I created two triggers:

-- trigger 1


DELIMITER $$
    CREATE TRIGGER atualizar_status_carro
      AFTER INSERT
      ON locacao
      FOR EACH ROW
     BEGIN 
        UPDATE carro
        SET car_status = 'INDISPONÍVEL'
        WHERE CAR_PLACA = NEW.LOc_CARRO;        
    END$$
DELIMITER ;$$

-- trigger 2

    DELIMITER $
    CREATE TRIGGER inserir_valor_locacao
      AFTER INSERT
      ON locacao
      FOR EACH ROW
    BEGIN 
        UPDATE locacao
        SET valoraloc = (select car_valor from carro where car_placa = NEW.loc_carro)
        WHERE loc_cod = NEW.loc_cod;        
    END$
    DELIMITER ;$

After creating the triggers and trying to insert the LOCATION:

INSERT INTO locacao(dtaluguel, qtde_dias, loc_cliente, loc_carro, loc_usuario, loc_pagamento)
    VALUES ('2018-02-21', 10, '5222222855','HHKU1555', 1, 1);

This error appears, and my question is how can I solve this problem: Error:

Error Code: 1442. Can't update table 'locacao' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.   0.063 sec
    
asked by anonymous 19.02.2018 / 01:24

0 answers