AFTER DELETE Triger
توضیحات
AFTER DELETE Trigger به معنی این است که بعد از اجرای یک دستور delete، یک تریگر فعال (fire) شود.
دستور
دستور Ater delete triger به صورت زیر نوشته میشود:
| ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ |
CREATE [ OR REPLACE ] TRIGGER trigger_nameAFTER DELETE ON table_name [ FOR EACH ROW ]DECLARE -- variable declarationsBEGIN -- trigger codeEXCEPTION WHEN ... -- exception handlingEND; |
.
OR REPLACE: گزینهای اختیاری است، اگر فعال شود به این معنی است که میتوان دوباره تریگری با این نام ساخت بدون اینکه نیاز باشد این تریگر پاک شود.
table_name: نام جدولی که تریگر بر روی آن ساخته شده است.
نکته: این دستور را میتوان برای insert و update به صورت after insert و after delete نوشت.
مثال
مثالی از دستور ساخت After Delete triger را مشاهده میکنیم:
| ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ ۳۵ ۳۶ ۳۷ ۳۸ ۳۹ |
CREATE TABLE orders( order_id number(۵), quantity number(۴), cost_per_item number(۶,۲), total_cost number(۸,۲));CREATE OR REPLACE TRIGGER orders_after_deleteAFTER DELETE ON orders FOR EACH ROWDECLARE v_username varchar۲(۱۰);BEGIN -- Find username of person performing the DELETE on the table SELECT user INTO v_username FROM dual; -- Insert record into audit table INSERT INTO orders_audit ( order_id, quantity, cost_per_item, total_cost, delete_date, deleted_by) VALUES ( :old.order_id, :old.quantity, :old.cost_per_item, :old.total_cost, sysdate, v_username );END; |
