enfield
enfield

Reputation: 841

MySQL Triggers: table to table

I need to write a trigger that will create a record in another table.

In my user table when a registering user responds to an activation email their status in that table changes from 0 to 1. When this change occurs I need it create a record in another table that has an auto-incrementing int primary id (Party).

Since the user status can be of three different states (not active (0), active (1), and banned (-1) I need this trigger to only set off when the status is changed from 0 to 1.

Can someone please help me with the SQL here?

enter image description here

Upvotes: 1

Views: 116

Answers (1)

The Scrum Meister
The Scrum Meister

Reputation: 30111

DELIMITER $$

CREATE TRIGGER users_status_change AFTER UPDATE on users
FOR EACH ROW BEGIN
    IF OLD.Status = 0 AND NEW.Status = 1 THEN
        INSERT Party(Name)
        VALUES('blar blar');
    END IF;
END;
$$

DELIMITER ;

Upvotes: 2

Related Questions