0% found this document useful (0 votes)
9 views3 pages

DML Triggers

The document explains triggers in databases, which are special stored procedures that execute automatically in response to specific events. It outlines three types of triggers: DML, DDL, and Logon triggers, with a focus on DML triggers that respond to insert, update, and delete operations. Additionally, it provides SQL examples for creating tables and triggers related to student records and their auditing process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

DML Triggers

The document explains triggers in databases, which are special stored procedures that execute automatically in response to specific events. It outlines three types of triggers: DML, DDL, and Logon triggers, with a focus on DML triggers that respond to insert, update, and delete operations. Additionally, it provides SQL examples for creating tables and triggers related to student records and their auditing process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TRIGGER

A trigger is a special kind of stored procedure that automatically executes when an event occurs
in the database server.

EXPLICITLY - MANUAL
IMPLICITLY - AUTOMATIC

There are 3 types of triggers


1. DML TRIGGERS (DATA MANIPULATION LANGUAGE) INSERT, UPDATE, DELETE
2. DDL TRIGGERS (DATA DEFINITION LANGUAGE) CREATE, ALTER, DROP
3. LOGON TRIGGERS

DML TRIGGERS ARE FIRED AUTOMATICALLY IN RESPONSE TO DML EVENTS


(INSERT, UPDATE AND DELETE)
DML TRIGGERS CAN BE OF 2 TYPES
1. AFTER TRIGGERS (ALSO CALLED FOR TRIGGERS)
2. INSTEAD OF TRIGGERS

create table tbl_Student (Id int primary key identity, Name varchar(50), Gender varchar(50),
Class int, fees int);
select * from tbl_Student;
insert into tbl_Student values('Osama','Male',10,4000);
insert into tbl_Student values('Anum','Female',9,3500);
insert into tbl_Student values('Talha','Male',8,3000);
insert into tbl_Student values('Maheen','Female',10,4000);
insert into tbl_Student values('Abdul','Male',9,3500);
insert into tbl_Student values('Mushtaq','Male',5,3000);
insert into tbl_Student values('Anas','Male',7,3000);
insert into tbl_Student values('Muneer','Male',8,4000);
insert into tbl_Student values('Momina','Female',9,3000);
insert into tbl_Student values('Aslam','Male',7,4000);
insert into tbl_Student values('Mumtaz','Female',6,3000);

create table tbl_Student_Audit


(Audit_Id int primary key identity, Audit_Info varchar(max));
select * from tbl_Student_Audit;

select * from tbl_Student;


update tbl_student set Name = 'Ali', Gender = 'Male'
where id = 12;

delete from tbl_Student where id = 9;


delete from tbl_Student where id = 10;
delete from tbl_Student where id = 13;

alter trigger tr_Student_forinsert


on tbl_Student
after insert
as
begin
select * from inserted
end

create trigger tr_Student_forDelete


on tbl_Student
after delete
as
begin
select * from deleted
end

create trigger tr_Student_forUpdate


on tbl_Student
after update
as
begin
select * from inserted
select * from deleted
end

sp_helptext tr_Student_audit_forinsert;

alter trigger tr_Student_audit_forinsert


on tbl_Student
after insert
as
begin
Declare @id int
Select @id = id from inserted
insert into tbl_Student_Audit
values('Student with id ' + Cast(@id as varchar(50)) + ' is added at ' + Cast(GETDATE() as
varchar(50)));
end

create trigger tr_Student_audit_forDelete


on tbl_Student
after delete
as
begin
Declare @id int
Select @id = id from deleted
insert into tbl_Student_Audit
values('Existing Student with id ' + Cast(@id as varchar(50)) + ' is deleted at ' + Cast(GETDATE()
as varchar(50)));
end

You might also like