AfterAcademy Tech
•
04 Mar 2020

Have you ever signed up for any website using your Gmail? Well of course yes! Sometimes when you sign up, you automatically get a welcome mail on your Gmail like "Hey, thank-you for registering to XYZ". How does this happen? One possibility is that the triggers may have been used. Whenever a new user data is entered into the website's database the trigger automatically sends a welcome mail to the new user. I hope now you got the basic idea of triggers. But what exactly are these triggers? In this blog, we will be learning about Triggers in DBMS. So, let's get started.
Triggers are the SQL statements that are automatically executed when there is any change in the database. The triggers are executed in response to certain events(INSERT, UPDATE or DELETE) in a particular table. These triggers help in maintaining the integrity of the data by changing the data of the database in a systematic fashion.
create trigger Trigger_name
(before | after)
[insert | update | delete]
on [table_name]
[for each row]
[trigger_body]
Suppose we have a table named Student containing the attributes Student_id, Name, Address, and Marks.

Now, we want to create a trigger that will add 100 marks to each new row of the Marks column whenever a new student is inserted to the table.
The SQL Trigger will be:
CREATE TRIGGER Add_marks
BEFORE
INSERT
ON Student
FOR EACH ROW
SET new.Marks = new.Marks + 100;
The new keyword refers to the row that is getting affected.
After creating the trigger, we will write the query for inserting a new student in the database.
INSERT INTO Student(Name, Address, Marks) VALUES('Alizeh', 'Maldives', 110);
The Student_id column is an auto-increment field and will be generated automatically when a new record is inserted into the table.
To see the final output the query would be:
SELECT * FROM Student;

This was about Triggers in SQL. If you have come this far, it means you liked what you are reading. Hope you learned something new today.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn how triggers are different from stored procedures.

AfterAcademy Tech
In this blog, we will learn how to cross-join two tables in DBMS, and what are the benefits of doing it with example.

AfterAcademy Tech
In this blog, we will learn what is a stored procedure in DBMS, and how it is executed depending on the number of parameters passed.

AfterAcademy Tech
In this blog, we will learn what is RDBMS and how it is different from DBMS. People often use these words intechangeably. But there is a difference between these two terms. So, let's learn how.
