CREATE TRIGGER trigger_name trigger_time trigger_event
This code was highlighted with the Source Code Highlighter .
- the table we will follow
CREATE TABLE `test` (
ʻid` INT (11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`content` TEXT NOT NULL
) ENGINE = MYISAM
- log
CREATE TABLE `log` (
ʻid` INT (11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`msg` VARCHAR (255) NOT NULL ,
` time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`row_id` int (11) NOT NULL
) ENGINE = MYISAM
- trigger
DELIMITER |
CREATE TRIGGER `update_test` AFTER INSERT ON` test`
FOR EACH ROW BEGIN
INSERT INTO log Set msg = 'insert' , row_id = NEW .id;
END ; * This source code was highlighted with Source Code Highlighter .
- Remove trigger
DROP TRIGGER `update_test`;
- Create another table,
- in which backup copies of rows from the test table will be stored
CREATE TABLE `testing`` backup` (
ʻid` INT (11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`row_id` int (11) unsigned NOT NULL ,
`content` TEXT NOT NULL
) ENGINE = MYISAM
- triggers
DELIMITER |
CREATE TRIGGER `update_test` before update ON` test`
FOR EACH ROW BEGIN
INSERT INTO backup Set row_id = OLD .id, content = OLD .content;
END ;
CREATE TRIGGER `delete_test` before delete ON` test`
FOR EACH ROW BEGIN
INSERT INTO backup Set row_id = OLD .id, content = OLD .content;
Source Code Highlighter .
Source: https://habr.com/ru/post/37693/
All Articles