adspace


What is trigger explain with example?

Answer Posted / Harminder Kaur

A trigger in SQL and PL/SQL is a special kind of stored procedure that automatically responds to certain events (such as insert, update, or delete) on a specific table. Triggers are used to enforce business rules, maintain data integrity, and perform auditing functions. An example of a trigger in SQL would be creating an AFTER INSERT trigger on the 'employees' table to automatically assign a unique ID to each newly inserted record:

```sql
CREATE TRIGGER auto_id_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
SET new.id = (SELECT MAX(id) + 1 FROM employees);
END;
```
In this example, the trigger 'auto_id_insert' is created to be activated after an insert operation on the 'employees' table. For each row inserted, the new value of 'id' will be set to the maximum 'id' from the 'employees' table plus one.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the current version of sql?

1106


Can we use distinct and group by together?

1146


What is the best sql course?

1062


How do I remove duplicates in two columns?

1202


What is the current version of postgresql?

1188


what is sql server agent? : Sql dba

1188


Can we rollback truncate?

1083


Is inner join faster than left join?

1287


how many tables will create when we create table, what are they? : Sql dba

1154


Does group by remove duplicates?

1120


how to start mysql server? : Sql dba

1279


Do we need to rebuild index after truncate?

1151


what is bcp? When does it used? : Sql dba

1065


What is your daily office routine?

2351


how to escape special characters in sql statements? : Sql dba

1124