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 |
how to fetch alternate records from a table? : Sql dba
What do you mean by table in sql?
How do I restart sql?
Why use triggers in sql?
Explain what is a column in a table?
how to select unique records from a table? : Sql dba
what are date and time intervals? : Sql dba
Is pl sql and postgresql same?
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?
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?
Oracle (3253)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)