Wyzwalacz (ang. trigger) – procedura wykonywana automatycznie jako reakcja na pewne zdarzenia w tabeli bazy danych. Wyzwalacze mogą ograniczać dostęp do pewnych danych, rejestrować zmiany danych lub nadzorować modyfikacje danych
CREATE TABLE trigger_test(
message VARCHAR (100)
);
DELIMITER $$
CREATE
TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
INSERT INTO trigger_test VALUES ('added new employee');
END $$
DELIMITER;
message VARCHAR (100)
);
DELIMITER $$
CREATE
TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
INSERT INTO trigger_test VALUES ('added new employee');
END $$
DELIMITER;
Before anything new item will be added to employee table than insert into trigger value "Added new employee"
DELIMITER change ; into $$ because ; need to be use in TRIGGER what cause end of function
DELIMITER $$
CREATE
TRIGGER my_trigger1 BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
IF NEW.sex='M' THEN
INSERT INTO trigger_test VALUES ('added male employee');
ELSEIF NEW.sex = 'F' THEN
INSERT INTO trigger_test VALUES ('added female');
ELSE
INSERT INTO trigger_test VALUES ('added other employee');
END IF;
END $$
DELIMITER;
INSERT INTO employee
VALUES(179, 'Oscar', 'Test', '1968-02-11', 'F', 6900, 106, 3);
INSERT INTO employee
VALUES(180, 'Oscar', 'Bob', '1968-02-11', 'M', 6900, 106, 3);
SELECT * FROM trigger_test;
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
SHOW TRIGGERS;
Komentarze
Prześlij komentarz