How to create an insert Trigger

Asked

Viewed 917 times

0

I need to assemble a trigger so that every time you insert a data into a table usuario, automatically insert the same data into another table bkuser. How can I do this? PHP Usage. And how do you invoke trigger?

  • 1

    I recommend reading http://www.devmedia.com.br/mysql-triggers/8088 and https://dev.mysql.com/doc/refman/5.5/en/create-trigger.html

  • Put there what you already have to help anyone who answers... (It was not I who gave -1, even because my votes today are over, only two hours from now rsrs)

  • 2

    So you were going to give -1 @gustavox ? Brinks kkkkk

  • 1

    Neither did I give -1. I prefer to talk to the OP about the question and give him another chance to improve it. :)

  • kkkkk or @Williamokano I was going to give +1 because I found it unfair. The question seemed interesting, pity that the AP does not bring any code, and I think that why took -1...

  • 1

    @gustavox hehe, I think I understand what he wanted... I just think. I did a post, hope it’s what he needs. Time for hunger games, flw

  • 3

    @Diegofelipe, I give -1 and ask ransom "If you improve your question, I return your reputation!" P

Show 2 more comments

1 answer

2


Trigger is related to the database and not the language.

A Rigger is created just to be automatically invoked before or after something.

For example, I can create a log for audit, always after updating some record in the table that has Trigger. Example:

create table usuarios (
    id int primary key auto_increment,
    nome varchar (45) not null
);

create table usuarios_alteracoes_historico (
    id_usuario int not null,
    nome varchar (45) not null,
    constraint fk_usuario_historico_alteracao foreign key (id_usuario) references usuarios (id)
);

CREATE TRIGGER usuarios_salva_historico
AFTER UPDATE ON usuarios
FOR EACH ROW BEGIN
INSERT INTO usuarios_alteracoes_historico (id_usuario, nome) VALUES
(NEW.id, OLD.nome); END//

insert into usuarios (nome) values
('william'), ('roberto');

update usuarios set nome = 'william okano' where nome = 'william';

select * from usuarios;
select * from usuarios_alteracoes_historico;

In the case to "activate the Trigger" you just have to make an Insert in the table users that will be "transparent" for you to insert in the history table.

It’s up to you now to adapt to what you need, as your question is poorly elaborated and confused.

  • +1 But he can still improve the question, and to make sense you will have to change the answer! : P

  • Resolved thank you

Browser other questions tagged

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