Followers

MySql Triggers

 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:

  1. Before Insert
    :
     It is activated before the insertion of data into the table.
  2. After Insert
    :
     It is activated after the insertion of data into the table.
  3. Before Update
    :
     It is activated before the update of data in the table.
  4. After Update
    :
     It is activated after the update of the data in the table.
  5. Before Delete
    :
     It is activated before the data is removed from the table.
  6. After Delete
    :
     It is activated after the deletion of data from the table.
  1. CREATE TABLE employee(  
  2.     name varchar(45) NOT NULL,    
  3.     occupation varchar(35) NOT NULL,    
  4.     working_date date,  
  5.     working_hours varchar(10)  
  6. );  
    1. INSERT INTO employee VALUES    
    2. ('Robin''Scientist''2020-10-04', 12),  
    3. ('Warner''Engineer''2020-10-04', 10),  
    4. ('Peter''Actor''2020-10-04', 13),  
    5. ('Marco''Doctor''2020-10-04', 14),  
    6. ('Brayden''Teacher''2020-10-04', 12),  
    7. ('Antonio''Business''2020-10-04', 11);  

    Next, execute the SELECT statement to verify the inserted record:

  1. mysql> DELIMITER //  
  2. mysql> Create Trigger before_insert_empworkinghours   
  3. BEFORE INSERT ON employee FOR EACH ROW  
  4. BEGIN  
  5. IF NEW.working_hours < 0 THEN SET NEW.working_hours = 0;  
  6. END IF;  
  7. END //  
  8. Now, we can use the following statements to invoke this trigger:

    1. mysql> INSERT INTO employee VALUES    
    2. ('Markus''Former''2020-10-08', 14);  
    3.   
    4. mysql> INSERT INTO employee VALUES    
    5. ('Alexander''Actor''2020-10-012', -13);  

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: MySql Triggers
MySql Triggers
DevOpsWorld
https://www.devopsworld.co.in/2022/04/mysql-triggers.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/04/mysql-triggers.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content