Bing Gan
Bing Gan

Reputation: 455

Is it possible to let mysql update itself by changing other tables?

Suppose I have a table1 containing variables called: v1, v2, v3, etc.

And I have another table2 containing variables called: y1, y2, y3, etc.

where y = f(v_s). v_s means some of the v1, v2, v3, etc.

When I update some value, let's say I added a new set of data of v_s into table1, is it possible for mysql to update y_s automatically?

Also I'm using python to maneuver my database. Just FYI.

Thanks!

Upvotes: 0

Views: 46

Answers (1)

Darwin von Corax
Darwin von Corax

Reputation: 5256

This is fairly easy to do using triggers. In your case the code would look something like

CREATE TRIGGER insert_table2
  BEFORE INSERT ON table1
  FOR EACH ROW
    INSERT INTO table2
        (y1, y2, y3)
      VALUES
        (f(NEW.v1), f(NEW.v2), f(NEW.v3));

The reference to NEW.v1 refers to the value of v1 in the newly-inserted row.

Upvotes: 1

Related Questions