SQLite - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQLite Triggers

SQLite Triggers

shape Description

SQL Triggers are SQLite programs, which can be executed automatically by the Oracle Server when an event is raised. A trigger is executed before or after an insert, update or delete statement. SQLite Triggers are fired, when a appropriate event occurs for the table. SQLite Triggers are used to perform verification checks on data values for insertion and perform calculations. The types of SQLite Triggers are:

Before Insert trigger

shape Description

SQLite Before insert trigger will fire the trigger before performing insert records values inside the table.

shape Syntax

create trigger <trigger_name> before insert on <table_name> for each row BEGIN -- variable declarations -- trigger code END;
trigger_name =>The name of the trigger to be create. table_name =>The accurate table in the database.

shape Conceptual figure

shape Examples

By viewing the below example, the concept of before insert trigger easily understands. [sql] sqlite> select * from employee01; 1 | david | delhi | manager | 12000 | 855 2 | shaha | mumbai | assistant manager | 13000 | 853 3 | sha | puna | scales manager | 11000 | 850 4 | jack | bangalore | designer | 14000 | 854 5 | james | mangalore | web designer | 15000 | 1124 sqlite> delimiter $$ sqlite> CREATE TRIGGER inst_trigger BEFORE INSERT ON EmpLOYEE01 -> FOR EACH ROW -> BEGIN -> UPDATE Employee01 SET Salary=Salary-400 where perks>500; -> end; -> $$ sqlite> delimiter ; sqlite> insert into employee01 values(6,'mike','chennai','develpoer',15500,840); sqlite> select * from employee01; 1 | david | delhi | manager | 11600 | 855 2 | shaha | mumbai | assistant manager | 12600 | 853 3 | sha | puna | scales manager | 10600 | 850 4 | jack | bangalore | designer | 13600 | 854 5 | james | mangalore | web designer | 14600 | 1124 6 | mike | chennai | develpoer | 15100 | 840 [/sql] In the above example, before insert, trigger will fire the condition before inserting a record inside the table or on the table, and update command is performed on the column salary(Set Salary=Salary-400 where perks>500). Then the salary of all the employee changes as all the employee perks are greater then the applied condition.

Before update trigger

shape Description

SQLite Triggers Before update will fired the trigger before performing update records values inside the table.

shape Syntax

create trigger <trigger_name> before update on <table_name> for each row BEGIN — variable declarations — trigger code END;
trigger_name =>The name of the trigger to be create. table_name =>The accurate table in the database.

shape Examples

By viewing the below example, the concept of before update trigger easily understands. [sql] sqlite> select * from employee01; 1 | david | delhi | manager | 12000 | 855 2 | shaha | mumbai | assistant manager | 13000 | 853 3 | sha | puna | scales manager | 11000 | 850 4 | jack | bangalore | designer | 14000 | 854 5 | james | mangalore | web designer | 15000 | 1124 6 | mike | chennai | develpoer | 15500 | 840 sqlite> delimiter // sqlite> CREATE TRIGGER updtrigger BEFORE UPDATE ON Employee01 -> FOR EACH ROW -> BEGIN -> IF NEW.Salary=500 THEN -&gt; SET NEW.Salary=10000; -> ELSEIF NEW.Salary>500 THEN -> SET NEW.Salary=15000; -> END IF; -> END -> // sqlite> delimiter ; sqlite> UPDATE Employee01 -> SET Salary=500; sqlite> select * from employee01; 1 | david | delhi | manager | 10000 | 855 2 | shaha | mumbai | assistant manager | 10000 | 853 3 | sha | puna | scales manager | 10000 | 850 4 | jack | bangalore | designer | 10000 | 854 5 | james | mangalore | web designer | 10000 | 1124 6 | mike | chennai | develpoer | 10500 | 840 [/sql] In the above example, before update trigger will fire the condition before updating a record inside the table or on the table,and update command is performed on the column salary(update if Salary=Salary>11000 and perks>500 then change to 10000).Then the salary of all the employee changes,as all the employee perks are greater then the applied condition.

After insert trigger

shape Description

SQLite After Insert trigger will fire the trigger after performing insert records values inside the table.

shape Syntax

create trigger <trigger_name> AFTER INSERT ON <table_name> FOR EACH ROW BEGIN -- variable declarations -- trigger code END;
trigger_name => The name of the trigger to be create. table_name => The accurate table in the database.

shape Examples

By viewing the below example, the concept of SQLite Trigger "after insert" easily understands. [sql] sqlite create table student_table(stu_id int,stu_name varchar(255),stu_class int); sqlite> create table student_log1(user_id varchar(255),description varchar(255)); sqlite> CREATE TRIGGER student_insert AFTER insert ON student_table FOR EACH ROW BEGIN INSERT into student_log1(user_id,description) VALUES (user(),CONCAT('INSERT student records',new.stu_id,' ',new.stu_name,' ',new.stu_class)); END$$ sqlite> delimiter; sqlite> insert into student_table values(1,'mike',10); sqlite> insert into student_table values(2,'mad',20); sqlite> insert into student_table values(3,'mack',30); sqlite> select * from student_log1; root@localhost | INSERT student records1 mike 10 root@localhost | INSERT student records2 mad 20 root@localhost | INSERT student records3 mack 30 [/sql] In the above example, after insert, trigger will fire the condition after inserting a record inside the table or on the table i.e, after creating student_insert table, it will fire on the student_log1 table and all the values will display on student_log1 table which was inserted in student_table.

After update trigger

shape Description

SQLite After update trigger will fire the trigger after performing insert records values inside the table.

shape Syntax

create trigger <trigger_name> AFTER UPDATE ON <table_name> FOR EACH ROW BEGIN -- variable declarations -- trigger code END;
trigger_name => The name of the trigger to be create. table_name => The accurate table in the database.

shape Examples

By viewing the below example, the concept of SQLite Triggers "after update" easily understands. [sql]sqlite>Create Table Student_Table02( Stu_Id int, Stu_Name varchar(255),Stu_Class int); create table stu_log02( user_id VARCHAR(255), description VARCHAR(255)); delimiter $$ sqlite> CREATE TRIGGER stu_update ->AFTER UPDATE ON stu_table FOR EACH ROW ->BEGIN ->INSERT into stu_log(user_id, description) -> VALUES (user(), CONCAT('Update Student Record ->(',old.stu_id,' ',old.stu_name,' ',old.stu_class,') to (',new.stu_id,' ',new.stu_name,'',new.stu_class,')')); ->END$$ sqlite> delimiter ; sqlite> select * from student_table02; 1 | david | 9 2 | shah | 9 3 | mike | 9 4 | james | 9 sqlite> update student_table02 set stu_class=stu_class+1; sqlite> select * from student_table02; 1 | david | 10 2 | shah | 10 3 | mike | 10 4 | james | 10 sqlite>select * from stu_log02; root@localhost | Update Student Record (1 david 9) to (1 david 10) root@localhost | Update Student Record (2 shah 9) to (2 shah 10) root@localhost | Update Student Record (3 mike 9) to (3 mike 10) root@localhost | Update Student Record (4 james 9) to (4 james 10) [/sql] In the above example, after update trigger will fire the condition after update a record inside the table or on the table. i.e,after creating student_update table it will fire on the student_log2 table and all the values will display on student_log2 table which was inserted in student_table02.

Summary

shape Key Points

  • SQLite Triggers - A trigger automatically executes when an event is raised.
  • SQLite Before Insert Trigger - Before Insert Trigger fire before inserting the values.
  • Before Update Trigger - Before Insert Trigger fire before updating the values.
  • After Insert Trigger - After Insert Trigger fire after inserting the values.
  • After Update Trigger - After Update Trigger fired updating the values.