A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for maintaining the integrity of the information on the database. For example, when a new record (representing a new worker) is added to the employees table, new records should also be created in the tables of the taxes, vacations and salaries.
CREATE TABLE table1(field1 CHAR); CREATE TABLE table2(field2 CHAR); CREATE TABLE table3(field3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY); CREATE TABLE table4( field4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY, field5 INT DEFAULT 0 ); delimiter | CREATE TRIGGER tg_table1 BEFORE INSERT ON table1
FOR EACH ROW BEGIN INSERT INTO table2 SET field2 = NEW.field1; DELETE FROM table3 WHERE field3 = NEW.field1; UPDATE table4 SET field4 = field5 * 10 WHERE field5 = NEW.field1; END; |
0 comments
Post a Comment