Skip to main content

Welcome to DBA Master – Database Tips, Tricks, and Tutorials

Welcome to DBA Master ! This blog is dedicated to all things related to database administration , SQL optimization , and performance tuning . Whether you're a beginner or a seasoned DBA, you'll find practical guides, troubleshooting tips, and real-world tutorials to help you work smarter with data. What to Expect: SQL performance tuning tips Indexing strategies Backup and recovery best practices High availability and replication techniques Database creation, configuration, and setup Monitoring queries and scripts for proactive performance management Migration guides across different database platforms Security essentials and best practices Recommended tools for DBAs Real-world error fixes and how to solve them Stay tuned — exciting content is coming soon. Feel free to bookmark and share: www.dbamaster.com ! Thanks for visiting!

PLSQL Trigger

Trigger is a PL/SQL block that is automatically executed in response to specific events.

Features:

  1. Automatic Execution: Triggers are automatically executed when the event that they are defined on occurs, without user intervention.
  2. Event-Driven: Triggers can be fired by various events like INSERT, UPDATE, or DELETE on a table or view.
  3. Before and After Triggers: Triggers can be executed either before the triggering event (BEFORE TRIGGER) or after the event (AFTER TRIGGER).
  4. Row-level triggers execute once for each row affected by the DML statement.
  5. Statement-level triggers execute only once per statement, regardless of how many rows are affected.
  6. Conditional Logic: Triggers can include conditional logic to control when they fire based on the data in the row being affected.
  7. Database Events: Triggers can respond to DDL statements (like CREATE, ALTER, DROP) and database system events (e.g., logon, logoff).

Trigger Events:

Database manipulation (DML)

Statement Level
Before insert, before update, before delete, after insert, after update, after delete
Row Level (on each ROW)
Before insert, before update, before delete, after insert, after update, after delete

Database definition (DDL)

Create, alter, drop, truncate, drop, rename, grant, revoke, analyze, audit, noaudit, comment, etc.

Database System operation

After startup, after login, before logoff, after suspend, before shutdown, after db_role_change, after servererror.

Examples:

CREATE TABLE employees (
employee_id  NUMBER GENERATED BY DEFAULT AS IDENTITY,
first_name   VARCHAR2(50),
last_name    VARCHAR2(50),
department_id NUMBER,
salary       NUMBER(10, 2),
hire_date    DATE,
CONSTRAINT pk_employees PRIMARY KEY (employee_id)
);


CREATE TABLE departments (
department_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
department_name VARCHAR2(100),
location VARCHAR2(100),
CONSTRAINT pk_departments PRIMARY KEY (department_id)
);

CREATE TABLE salary_log (
log_id      NUMBER GENERATED BY DEFAULT AS IDENTITY,
employee_id NUMBER,       
old_salary  NUMBER(10, 2),
new_salary  NUMBER(10, 2),
change_date DATE,
CONSTRAINT pk_salary_log PRIMARY KEY (log_id)    
);

CREATE TABLE audit_log (
log_id      NUMBER GENERATED BY DEFAULT AS IDENTITY,
action      VARCHAR2(50),
table_name  VARCHAR2(50),
action_date DATE,  
CONSTRAINT pk_audit_log PRIMARY KEY (log_id)     
);


CREATE TABLE user_logon_audit (
log_id      NUMBER GENERATED BY DEFAULT AS IDENTITY,
username    VARCHAR2(50),
logon_time  DATE,
CONSTRAINT pk_user_logon_audit PRIMARY KEY (log_id)         
);

Statement-Level Trigger:

CREATE OR REPLACE TRIGGER trg_departments_update
AFTER UPDATE ON departments
BEGIN

    -- Record the update in an audit table
    INSERT INTO audit_log (action, table_name, action_date)
    VALUES ('UPDATE', 'DEPARTMENTS', SYSDATE);
END;
/

Row-Level Trigger:

CREATE OR REPLACE TRIGGER trg_employee_salary_update
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
BEGIN

    -- Log the change in salary
    INSERT INTO salary_log (employee_id, old_salary, new_salary, change_date)
    VALUES (:OLD.employee_id, :OLD.salary, :NEW.salary, SYSDATE);
END;
/

Database System-Level Trigger:

CREATE OR REPLACE TRIGGER trg_logon_audit
AFTER LOGON ON DATABASE
BEGIN

    -- Log the login event
    INSERT INTO user_logon_audit (username, logon_time)
    VALUES (USER, SYSDATE);
END;
/

Enable and Disable  the triggers:

alter trigger trigger_name disable;
alter trigger trigger_name  enable;

Enable and Disable  the All triggers:

alter table orders enable all triggers;
alter table orders disable all triggers;

Drop a Trigger:

drop trigger trigger_name;

List Out the Triggers:

SELECT trigger_name, status, trigger_type, table_name FROM user_triggers;
SELECT owner, trigger_name, status, trigger_type, table_name FROM all_triggers;
select * from dba_triggers;

Trigger Restrictions:

  1. Mutating Tables: Cannot query or modify the table that is being modified in the triggering statement.
  2. No COMMIT/ROLLBACK: Triggers cannot contain explicit COMMIT or ROLLBACK statements.
  3. Limited SQL Operations: DDL statements (e.g., CREATE, ALTER, DROP) and modifications to the triggering table within row-level triggers are not allowed.
  4. Timing Restrictions: Cannot have both BEFORE and AFTER triggers for the same event on a table.
  5. Execution Order: The order of execution for multiple triggers on the same event is not guaranteed.
  6. Performance Considerations: Triggers can impact performance, especially with complex logic.
  7. Compound Triggers: Can help avoid mutating table issues and consolidate logic, but still subject to limits.
  8. Number of Triggers: Up to 12 triggers per DML operation type on a single table.

Comments

Popular posts from this blog

Oracle Database 19C Performance Tunning - PART 1

Advantages: 1. Improved Query Performance •    Optimized SQL execution plans lead to faster query response times. •    Reduces unnecessary full table scans and improves indexing strategies. •    Parallel execution tuning speeds up large data processing tasks. 2. Better Resource Utilization •    Efficient use of CPU, memory, disk I/O, and network resources. •    Reduces contention on Redo Logs, Undo Tablespaces, and Buffer Cache. •    Helps in load balancing across multiple instances in RAC (Real Application Clusters). 3. Increased System Scalability •    Ensures that the database can handle a growing number of users and transactions. •    Proper tuning allows scaling without degrading performance. •    Optimized parallel processing ensures better performance on multi-core servers. 4. Lower Infrastructure Costs •    Reduces the need for add...

Oracle RMAN Backup And Restore

RMAN: (Oracle 8) RMAN (Recovery Manager) is a utility provided by Oracle Database to perform backup, restore, and recovery operations. It is a command line tool. Features of RMAN in Oracle 19c Comprehensive Backup Capabilities: Full and incremental backups. Block-level backups for efficient data storage. Archived redo log backups. Fast Recovery Area (FRA) integration for centralized backup storage. Efficient Recovery Options: Point-in-time recovery (PITR). Complete and incomplete recovery. Flashback database capabilities for quick undo of changes. Multitenant Database Support: RMAN fully supports container databases (CDBs) and pluggable databases (PDBs). Provides flexibility to back up and recover individual PDBs or entire CDBs. Automatic Space Management: Manages disk space in the FRA. Automatically deletes obsolete backups and archived logs. Data Deduplication and Compression: Backup optimization through block-level deduplication. Built-in compression algorithms to reduce storage req...

Oracle 19c Database Software Installation in OEL8

 Pre-requisites for OS level:            Set the static IP Address     Disable the Firewall (systemctl stop firewalld & systemctl disable firewalld)     set SELINUX=permissive on /etc/selinux/config  ##Need to restart the server use init 6 Oracle Installation Pre-requisites Methods     Automatic Setup     Manual Setup      Automatic requisites Setup: (avoid step 1 to step 5): dnf install -y oracle-database-preinstall-19c Install the dependencies: curl -o oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64/getPackage/oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm dnf -y localinstall oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm Manual Setup: step 1: Add the karenl parameters and values vi /etc/sysctl.conf     fs.file-max = 6815744 kernel.sem = 250 32000 100 128 kernel....