Using CREATE TRIGGER statement you can create a trigger. You have to put the trigger name after the trigger statement.
For example we have two tables i.e. users and userhistory.
users:
userID
firstName
lastName
userhistory:
userID
last_update
Read Also: INSERT … ON DUPLICATE KEY UPDATE
Now we will create a trigger before update users table:
DELIMITER $ CREATE TRIGGER before_user_update BEFORE UPDATE ON users FOR EACH ROW BEGIN INSERT INTO userhistory SET last_update = NOW(), userID = OLD.userID; END$ DELIMITER ;
Whenever user update their information userhistory table also get updated automatically.
Triggers could be defined on the table, schema, view or database with which the event is associated.
Triggers can be written for the following purposes:
Are you an owner of a small business who’s trying to come up with ways…
Introduction When patients bring me their lab reports, the confusion is almost always the same.…
Are you excited about remodeling your house after a long time? Perhaps if you're planning…
The practice of yoga teaches us to be present, patient and mindful of our decisions.…
As we are once again allowed to go outside, nearly everyone is planning to go…
How do you find a cleaning company that will take excellent care of your business…