1

On my server, after insert on one table, I want to update two other tables. How can I do this using trigger?

Table one has two columns tab2id, tab3id.

After insert on tab1, I want to update the row in tab2 with tab2id and the row in tab3 with tab3id.

6
  • Add some more meaning to your question, give table structure, Give example you tried.. expected output Commented Jan 21, 2014 at 6:51
  • At least post schemas for all three tables and explain what exactly should be updated. Commented Jan 21, 2014 at 6:53
  • You should have one value for something in your entire database, there shouldn't be anywhere where your data is repeated. This is bad design. What are you using to update your database? Commented Jan 21, 2014 at 6:53
  • @dayuloli After insert on tab1, i want to update the row in tab2 with tab2id and the row in tab3 with tab3id. Commented Jan 21, 2014 at 6:57
  • @user2798694 Then can you not just write that in your MySQL query? Commented Jan 21, 2014 at 6:59

2 Answers 2

2

It hard to tell what exactly you want to update in tables tab2 and tab3 since you didn't elaborate on this in your question, but your trigger might look something like

DELIMITER $$ CREATE TRIGGER tg_ai_tab1 AFTER INSERT ON tab1 FOR EACH ROW BEGIN UPDATE tab2 SET value2 = value2 + 1 WHERE tab2id = NEW.tab2id; UPDATE tab3 SET value3 = value3 + 1 WHERE tab3id = NEW.tab3id; END$$ DELIMITER ; 

Here is SQLFiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

you sir are a genius, all i was missing is the begin ... and. thanks!
0

I don't think you can INSERT and do an UPDATE at the same time using 1 SQL statement. You need to separate the executions by creating individual SQL statements. If this information is not enough, check this out. It should answer your problem completely.

ACCESS/SQL: How to INSERT INTO/UPDATE at the same time?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.