Database Triggers and event attributes--Introduction[Blog 搬家]
Database Triggers and event attributes--Introduction
Introduction
Creation of Database Triggers
A database trigger is created and dropped with the following commands:
CREATE OR REPLACE TRIGGER trigger_name (BEFORE|AFTER)
database_trigger_event ON (DATABASE|schema.SCHEMA)…;
DROP TRIGGER trigger_name;
When a database trigger is created, the trigger is checked for syntax, the dependency tree and privileges are checked, and then the trigger is compiled into pcode and stored in the database. Therefore, triggers are similar to stored packages and procedures in the sense of creation, storage, and execution of pcode. The main differences are that database triggers source code is stored in a different data dictionary table and database triggers execute implicitly based on actions, whereas, packages and procedures are explicitly called. If errors occur during the creation or compilation of a database trigger, then the trigger is still created and enabled. If a database event executes that causes the database trigger to execute, the database event will fail. Therefore, if an error occurs during creation or compilation, the trigger needs to be either dropped, fixed and re-created, or disabled to ensure that processing does not stop. To view errors, the SHOW ERRORS command can be executed or the errors can be retrieved from the USER_ERRORS data dictionary view.
Security of Database Triggers
In order to create a database trigger, the schema must have one of 3 Oracle system privileges:
- CREATE TRIGGER: this privilege allows for a schema to create a database trigger on a table they own.
- CREATE ANY TRIGGER: this privilege allows a schema to create a database trigger on a table owned by another schema.
- ADMINISTER DATABASE TRIGGER: this privilege allows a schema to create a database wide database trigger.
Once a trigger is created, it is executed implicitly. Internally, Oracle fires the trigger in the existing user transaction.
Triggers are the same as stored packages and procedures and therefore, have dependencies that can cause a trigger to become invalidated. Any time a referenced stored package or procedure is modified, the trigger becomes invalidated. If a trigger ever becomes invalidated, then Oracle will attempt to internally re-compile the trigger the next time it is referenced. As a standard, a trigger that becomes invalidated, should be recompiled manually to ensure that the trigger will compile successfully. To compile a trigger manually, the ALTER TRIGGER command is used. This is shown below:
ALTER TRIGGER logon_trigger COMPILE;
To recompile a trigger, you must either own the trigger or have the ALTER ANY TRIGGER system privilege.
Enabling and Disabling Database Triggers
Disabled database triggers are companions to invalid objects. In some respects, a disabled trigger is far more dangerous than an invalid object because it doesn’t fail; it just doesn’t execute! This can have severe consequences for applications (and, consequently, for business processes) that depend on business logic stored within procedural code in database triggers. For this reason, you MUST run the following script. regularly to ensure there are not any disabled triggers that you are not aware of:
SELECT trigger_name, trigger_type, base_object_type,
triggering_event
FROM user_triggers
WHERE status <> 'ENABLED'
AND db_object_type IN ('DATABASE ', 'SCHEMA')
ORDER BY trigger_name;
TRIGGER_NAME TRIGGER_TYPE BASE_OBJECT_TYPE TRIGGERING_EVEN
------------------- ------------- ---------------- ---------------
DB_STARTUP_TRIGGER AFTER EVENT DATABASE STARTUP
Once the triggers are identified, they can be enabled manually or a dynamic SQL or PL/SQL script. can be created to build the SQL statements to ENABLE the triggers. To enable database triggers, the following three commands could be executed.
ALTER TRIGGER db_startup_trigger ENABLE; -- enabling a database trigger
ALTER TRIGGER before_insert_customer ENABLE; -- enabling a table trigger
ALTER TABLE s_customer ENABLE ALL TRIGGERS; -- enabling all triggers on a table
The preceding commands allow you to enable one trigger at a time or all the triggers on a table. To enable all triggers under a schema, the following script. can be used to build an ENABLE script. dynamically:
SET HEADING OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SELECT 'ALTER TRIGGER ' || trigger_name || ' ENABLE;'
FROM user_triggers
ORDER BY table_name;
ALTER TRIGGER DB_STARTUP_TRIGGER ENABLE;
ALTER TRIGGER BEFORE_INSERT_CUSTOMER ENABLE;
ALTER TRIGGER BEFORE_UPDATE_CUSTOMER ENABLE;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/34596/viewspace-661918/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Database Triggers and event attributes--Event Attributes【Blog 搬家】Database
- Database Triggers and event attributes--Database System Events【Blog 搬家】Database
- Database Triggers and event attributes--IntroductionDatabase
- Database Triggers and event attributes--DDL/Client Events【Blog 搬家】Databaseclient
- oracle Database Event trace 設定【Blog 搬家】OracleDatabase
- float datatype in Oracle database 【Blog 搬家】OracleDatabase
- Database Triggers and event attributes--Event AttributesDatabase
- Database Triggers and event attributes--Database System EventsDatabase
- Oracle Database Link Problems【Blog 搬家】OracleDatabase
- Oracle event 10231 【Blog 搬家】Oracle
- Database Triggers and event attributes--DDL/Client EventsDatabaseclient
- Database Link 建立注意的兩點【Blog 搬家】Database
- Oracle System Event TriggersOracle
- Oracle backgroud Process【Blog 搬家】Oracle
- oracle Distinct|Unique 異同【Blog 搬家】Oracle
- Oracle DDL 執行過程【Blog 搬家】Oracle
- HP -Data Protector Restore file system【Blog 搬家】REST
- V$SESSION_LONGOPS bug 【Blog 搬家】SessionGo
- 限制End User Session數量 【Blog 搬家】Session
- Trim() 函式的介紹【Blog 搬家】函式
- Oracle Date Function 講解和事例【Blog 搬家】OracleFunction
- ora -03232 問題解決【Blog 搬家】
- oracle ora-00997 problems【Blog 搬家】Oracle
- RMAN 只備份當前資料?【Blog 搬家】
- exp/imp expdp/impdp Tables 萬用字元 % 的使用【Blog 搬家】字元
- Oracle Optimizer -RBO (理解Rule-based 優化器)【Blog 搬家】Oracle優化
- oracle Database Event trace 設定OracleDatabase
- 資料庫加密Product_user_profile(PUP) TABLE FOR security 【Blog 搬家】資料庫加密
- MM-DD-RRRR / MM-DD-YYYY 的區別【Blog 搬家】
- 說服您的CTO使用事件溯源 -Event Store Blog事件
- Oracle10g V$OSSTAT 關於PHYSICAL_MEMORY_BYTES 的 Bug【Blog 搬家】Oracle
- oracle 9i database 在win2k server上搬家(轉摘)OracleDatabaseServer
- Triggers 授權
- 搬家成功了!
- 圖靈搬家啦!圖靈
- Oracle 21C TriggersOracle
- zabbix Triggers Top 100
- Log Switch Triggers 及Force SCN