"A Trigger is a Database object just like a stored procedure or we can say it is a special kind of Stored Procedure which fires when an event occurs in a database.".
It is a database object that is bound to a table and is executed automatically. We cannot explicitly call any trigger. Triggers provide data integrity and used to access and check data before and after modification using DDL or DML query.
There are two types of Triggers:
DDL Trigger
DML trigger
DDL Triggers: They fire in response to DDL (Data Definition Language) command events that start with Create, Alter and Drop like Create_table, Create_view, drop_table, Drop_view and Alter_table.
Code of DDL Triggers:
create trigger saftey on database for
create_table, alter_table, drop_table as print 'you can not create ,drop and alter table in this database' rollback;
Output:
DML Triggers: They fire in response to DML (Data Manipulation Language) command events that start with Insert, Update and Delete like insert_table, Update_view and Delete_table.
Code of DML Trigger:
create trigger deep on emp for insert, update, delete as print 'you can notinsert,update and delete this table I' rollback;
Output:
When we insert, update or delete in a table in a database then the following message appears: