Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

TRIGGER

Trigger is a statement that a system executes automatically when there is any modification to
the database. In a trigger, we first specify when the trigger is to be executed and then the
action to be performed when the trigger executes. Triggers are used to specify certain
integrity constraints and referential constraints that cannot be specified using the constraint
mechanism of SQL.
The trigger is always executed with the specific table in the database. If we remove the table,
all the triggers associated with that table are also deleted automatically.

In Structured Query Language, triggers are called only either before or after the below
events:

1. INSERT Event: This event is called when the new row is entered in the table.
2. UPDATE Event: This event is called when the existing record is changed or modified in
the table.
3. DELETE Event: This event is called when the existing record is removed from the table.

Example:

AFTER INSERT Trigger


This trigger is invoked after the insertion of data in the table.

AFTER UPDATE Trigger


This trigger is invoked in SQL after the modification of the data in the table.

AFTER DELETE Trigger


This trigger is invoked after deleting the data from the table.

BEFORE INSERT Trigger


This trigger is invoked before the inserting the record in the table.

BEFORE UPDATE Trigger


This trigger is invoked before the updating the record in the table.

BEFORE DELETE Trigger


This trigger is invoked before deleting the record from the table.

Syntax of Trigger in SQL


CREATE TRIGGER Trigger_Name

[ BEFORE | AFTER ] [ Insert | Update | Delete]


ON [Table_Name]
[ FOR EACH ROW | FOR EACH COLUMN ]
AS
Set of SQL Statement
In the trigger syntax, firstly, we have to define the name of the trigger after the CREATE
TRIGGER keyword. After that, we have to define the BEFORE or AFTER keyword with anyone
event.

Then, we define the name of that table on which trigger is to occur.

After the table name, we have to define the row-level or statement-level trigger.

And, at last, we have to write the SQL statements which perform actions on the occurring of
event.

Example of Trigger in SQL


To understand the concept of trigger in SQL, first, we have to create the table on which trigger
is to be executed.

The following query creates the Student_Trigger table in the SQL database:

CREATE TABLE Student_Trigger


(
Student_RollNo INT PRIMARY KEY,
Student_FirstName Varchar (100),
Student_EnglishMarks INT,
Student_PhysicsMarks INT,
Student_ChemistryMarks INT,
Student_MathsMarks INT,
Student_TotalMarks INT,
Student_Percentage INT);
The following query shows the structure of theStudent_Trigger table:
DESC Student_Trigger;
Output:

Field Type NULL Key Def Extra


ault

Student_RollNo INT NO PRI NULL

Student_FirstName Varchar(100) YES NULL

Student_EnglishMarks INT YES NULL

Student_PhysicsMarks INT YES NULL


Student_ChemistryMarks INT YES NULL

Student_MathsMarks INT YES NULL

Student_TotalMarks INT YES NULL

Student_Percentage INT YES NULL

The following query fires a trigger before the insertion of the student record in the table:
CREATE TRIGGER Student_Table_Marks
BEFORE INSERT
ON
Student_Trigger
FOR EACH ROW
SET new.Student_TotalMarks = new.Student_EnglishMarks + new.Student_PhysicsMarks
+ new.Student_ChemistryMarks + new.Student_MathsMarks,
new.Student_Percentage = ( new.Student_TotalMarks / 400) * 100;

Above SQL statement will create a trigger in the Student_Trigger in which whenever
subjects marks are entered, before inserting this data into the database, the trigger will
compute those two values and insert them with the entered values.

The following query inserts the record into Student_Trigger table:

INSERT INTO Student_Trigger (Student_RollNo, Student_FirstName, Student_EnglishMa


rks, Student_PhysicsMarks, Student_ChemistryMarks, Student_MathsMarks, Student_Tot
alMarks, Student_Percentage) VALUES ( 201, Sorya, 88, 75, 69, 92, 0, 0);

To check the output of the above INSERT statement, you have to type the following SELECT
statement:

SELECT * FROM Student_Trigger;

Output:

Studen Student_ Student_ Student Student Student Stude Student_


t_Roll FirstNam English _Physic _chemist _Maths nt_To Percenta
No e Marks sMarks ryMarks Marks talMa ge
rks

201 Sorya 88 75 69 92 324 81


Advantages of Triggers in SQL
Following are the three main advantages of triggers in Structured Query Language:

1. SQL provides an alternate way for maintaining the data in the tables.
2. They allow the database users to validate values before inserting and updating.

Disadvantages of Triggers in SQL


Following are the main disadvantages of triggers in Structured Query Language:

1. It is not possible to find and debug the errors in triggers.


2. If we use the complex code in the trigger, it makes the application run slower.
3. Trigger increases the high load on the database system.

Also used for protecting the data that was stored in the
database;

DDL Triggers

create tigger safety


on database
Forcreate_table,alter_table,drop_table
Asprint 'you can not create,drop and alter tab;

Output:

DML Triggers

create trigger deep


on emp
for
insert,update,delete
as
print 'you can not insert,update and delete this table'
rollback;
Output:

Complex Integrity Constraints in SQL With Examples


tutorialtpoint.net/2023/04/complex-integrity-constraints-in-sql.html#:~:text=On%20the
%20other%20hand%2C%20complex,are%20present%20in%20a%20table.

You might also like