Trigger to update another table

Asked

Viewed 1,825 times

0

I have 4 table in my Mysql database. -> companies -> contacts (company addresses) -> bank data (company’s bank data) -> members (members of the company )

All tables relate to the companies table. In the companies table I have an updated_at field, which I would like when updated any of the other 3 tables, this field was updated. I thought of a TRIGGER, but I don’t know how to do it.

1 answer

1


Thus:

CREATE TRIGGER socioup AFTER UPDATE
ON socios
FOR EACH ROW
BEGIN  
   UPDATE empresas SET updated_at = NOW() WHERE empresa_id = new.empresa_id;
END
  • Really that’s what I need, but it updates the updated_at of all companies... I tried to put something like: UPDATE companies SET updated_at = NOW() WHERE empresa_id = contacts.empresa_id ... but it didn’t work

  • Hmmmm. I didn’t know you have more than one company. Good. What’s in the partners table that defines the company?

  • i have a company column referencing company

  • And in the table companies which is the column that distinguishes companies?

  • also calls empresa_id

  • I changed the answer.

  • Ball show. But I didn’t understand how the new.empresa_id refers to the partners' table empresa_id. could explain me?

  • new.company is the value that is coming to Trigger is Trigger’s way of telling you what is new value of the empresa_id field since we are at the event AFTER THE UPDATE. Follow the logic and create two more for the other tables and you’re done.

Show 3 more comments

Browser other questions tagged

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