What is trigger explain with example?



What is trigger explain with example?..

Answer / 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

More SQL PLSQL Interview Questions

how to fetch alternate records from a table? : Sql dba

1 Answers  


What do you mean by table in sql?

1 Answers  


How do I restart sql?

1 Answers  


Why use triggers in sql?

1 Answers  


Explain what is a column in a table?

1 Answers  


how to select unique records from a table? : Sql dba

1 Answers  


what are date and time intervals? : Sql dba

1 Answers  


Is pl sql and postgresql same?

1 Answers  


I have 2 packages A and B. Now package A references Package B and Package B references Package A. How do you compile such inter-dependent objects in PL/SQL

5 Answers   Doyensys, Infosys, Metric Stream,


What is difference between CHAR and VARCHAR2?What is the maximum SIZE allowed for each type?

6 Answers   Saama Tech,


What is the difference between DELETE and TRUNCATE?

15 Answers   Johns Hopkins University, Tech Mahindra,


Can you have a foreign key without a primary key?

1 Answers  


Categories