create Trigger mysql

Asked

Viewed 402 times

0

I have two tables: the requested table and the logs table. Users can interact with the requested table, link a php script that runs a query update and update the status column. I need to create a Trigger that, when running this update query, saves the change in the log table.

I used this Rigger, but now does not update either the requested table or the log table:

        DELIMITER $$
        CREATE TRIGGER salva_status 
        BEFORE UPDATE ON pedidos
        FOR EACH ROW BEGIN
        INSERT INTO logs
        SET acao = 'update',
        nPedido = OLD.nPedido,
        status = OLD.status,
        modificadoem = NOW(); END$$
        DELIMITER ;

These are the query for table creation:

This table is the one that stores the order information

         CREATE TABLE `localhost`.`pedidos` ( `id` INT NOT NULL AUTO_INCREMENT , `emissaoPed` INT NOT NULL , 
        `nPed` INT NOT NULL , 
        `NrPedido` VARCHAR(20) NOT NULL ,
         `nomeAbrev` VARCHAR(50) NOT NULL , 
        `vlr` FLOAT NOT NULL , 
        `status` VARCHAR(20) NOT NULL , 
        UNIQUE (`id`)) ENGINE = InnoDB;

And this is the table that will store the logs:

        CREATE TABLE `root`.`logs` ( 
        `id` INT NOT NULL AUTO_INCREMENT , 
        `NrPedido` VARCHAR(20) NOT NULL , 
        `antigoStatus` VARCHAR(20) NOT NULL , 
        `novoStatus` VARCHAR(20) NOT NULL ,
         `modificadoem` DATE NOT NULL , 
        `vlr` FLOAT NOT NULL , 
        `status` VARCHAR(20) NOT NULL , 
        UNIQUE (`id`)) ENGINE = InnoDB;

  • Try to put AFTER UPDATE ON pedidos see if anything changes

  • I changed the query and no effect :/

  • But let me ask you, when you want to insert a record into a table through a Trigger, you don’t need to use the insert in accordance with that link https://www.techonthenet.com/mysql/triggers/after_update.php ???

  • Dude Try to change this Insert.... INSERT INTO logs(acao,nPedido,Status) VALUES ('update',OLD.nPedido, OLD.status, NOW());

  • sends the creates Tables I do to Trigger

  • 1

    Not INSERT INTO logs SET... is INSERT INTO logs VALUES or INSERT INTO logs (acao, nPedido, status, modificagem) VALUES (...)

  • So, guys, I put the query I used to create the tables to make it clearer

Show 2 more comments

1 answer

1


Try changing the INSERT:

INSERT INTO logs
        SET acao = 'update',
        nPedido = OLD.nPedido,
        status = OLD.status,
        modificadoem = NOW();

for

 INSERT INTO logs
          (acao, nPedido, status, modificagem)
        VALUES
          ('update', OLD.nPedido, OLD.status, NOW());
  • That’s what I was suggesting in the +1 comments

  • So R.Santos, didn’t work, but I put the query I used to create the tables, I think I was the one who ate ball, rs

  • FIXING: query worked, terms in my table that were not well defined

Browser other questions tagged

You are not signed in. Login or sign up in order to post.