A trigger in MySQL is a set of SQL statements that reside in a system catalog. It is a special type of stored procedure that is invoked aut...
A trigger in MySQL is a set of SQL statements that reside in a system catalog. It is a special type of stored procedure that is invoked automatically in response to an event. Each trigger is associated with a table, which is activated on any DML statement such as INSERT, UPDATE, or DELETE.
Types of Triggers in MySQL?
We can define the maximum six types of actions or events in the form of triggers:
- Before Insert: It is activated before the insertion of data into the table.
- After Insert: It is activated after the insertion of data into the table.
- Before Update: It is activated before the update of data in the table.
- After Update: It is activated after the update of the data in the table.
- Before Delete: It is activated before the data is removed from the table.
- After Delete: It is activated after the deletion of data from the table.
- CREATE TABLE employee(
- name varchar(45) NOT NULL,
- occupation varchar(35) NOT NULL,
- working_date date,
- working_hours varchar(10)
- );
Next, execute the SELECT statement to verify the inserted record:
- mysql> DELIMITER //
- mysql> Create Trigger before_insert_empworkinghours
- BEFORE INSERT ON employee FOR EACH ROW
- BEGIN
- IF NEW.working_hours < 0 THEN SET NEW.working_hours = 0;
- END IF;
- END //
Now, we can use the following statements to invoke this trigger:
COMMENTS