Trigger is a procedural code that executes whenever we perform INSERT, UPDATE or DELETE records in a table.
Trigger is very useful when we want to do any automatic execution such as modifying changes by related tables, applying column restrictions, result comparison on data modification and maintenance of referential integrity of the data across database.
For example, if we want to apply restriction for users to delete any record from dbo.emplyee_dtails table then following trigger can be used:
CREATE TRIGGER del_emplyee
ON dbo.emplyee_dtails
FOR DELETE
AS
BEGIN
ROLLBACK TRANSACTION
PRINT "You cannot delete any Employee!"
END
When anyone will try to delete record from the dbo.emplyee_dtails table then the del_emplyee trigger will cancel the deletion of the record and will roll back the transaction, also it will print a message "You cannot delete any Employee!"
Trigger is very useful when we want to do any automatic execution such as modifying changes by related tables, applying column restrictions, result comparison on data modification and maintenance of referential integrity of the data across database.
For example, if we want to apply restriction for users to delete any record from dbo.emplyee_dtails table then following trigger can be used:
CREATE TRIGGER del_emplyee
ON dbo.emplyee_dtails
FOR DELETE
AS
BEGIN
ROLLBACK TRANSACTION
PRINT "You cannot delete any Employee!"
END
When anyone will try to delete record from the dbo.emplyee_dtails table then the del_emplyee trigger will cancel the deletion of the record and will roll back the transaction, also it will print a message "You cannot delete any Employee!"
No comments:
Post a Comment