Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference before and after trigger in oracle

Tags:

sql

oracle10g

Can somebody explain difference between "before" and "after" trigger in oracle 10g with an example ?

like image 945
Jagan Avatar asked Sep 05 '10 12:09

Jagan


People also ask

What is difference between before trigger and after trigger?

Before Trigger is a type of trigger that automatically executes before a certain operation occurs on the table. In contrast, after trigger is a type of trigger that automatically executes after a certain operation occurs on the table.

What is difference between before update and after update?

All the code written in the "before update" triggers, executes BEFORE that DML is committed to Salesforce Database. Code written in after trigger executes AFTER the commit is made. Hence if you are trying to update any value in the record, on which the trigger is fired, you need not write an update call, specifically.

What are 3 types of SQL triggers?

SQL Server has three types of triggers: DML (Data Manipulation Language) Triggers. DDL (Data Definition Language) Triggers. Logon Triggers.

What is before trigger?

By using triggers that run before an update or insert, values that are being updated or inserted can be modified before the database is actually modified. These can be used to transform input from the application (user view of the data) to an internal database format where desired.


1 Answers

First, I'll start my answer by defining trigger: a trigger is an stored procedure that is run when a row is added, modified or deleted.

Triggers can run BEFORE the action is taken or AFTER the action is taken.

BEFORE triggers are usually used when validation needs to take place before accepting the change. They run before any change is made to the database. Let's say you run a database for a bank. You have a table accounts and a table transactions. If a user makes a withdrawal from his account, you would want to make sure that the user has enough credits in his account for his withdrawal. The BEFORE trigger will allow to do that and prevent the row from being inserted in transactions if the balance in accounts is not enough.

AFTER triggers are usually used when information needs to be updated in a separate table due to a change. They run after changes have been made to the database (not necessarily committed). Let's go back to our back example. After a successful transaction, you would want balance to be updated in the accounts table. An AFTER trigger will allow you to do exactly that.

like image 163
Andrew Moore Avatar answered Oct 08 '22 03:10

Andrew Moore