SlideShare a Scribd company logo
PL/SQL – Triggers
I Trigger non sono sottoprogrammi invocati esplicitamente,
ma vengono eseguiti in seguito al verificarsi di eventi.
Esempio
Si supponga di voler tenere traccia dei cambiamenti nella tabella books e di
avere una tabella book_hist:
CREATE TABLE books_hist
( isbn VARCHAR2(13) NOT NULL,
action CHAR(1) NOT NULL,
datestamp DATE NOT NULL,
oracle_user VARCHAR2(30) NOT NULL,
real_user VARCHAR2(60),
old_title VARCHAR2(200),
old_summary VARCHAR2(2000),
old_author VARCHAR2(200),
old_date_published DATE,
old_page_count NUMBER,
new_title VARCHAR2(200),
new_summary VARCHAR2(2000),
new_author VARCHAR2(200),
new_date_published DATE,
new_page_count NUMBER,
CONSTRAINT action_ck CHECK (action IN ('I', 'D', 'U')));
Esempio TriggerCREATE OR REPLACE TRIGGER book_hist_trg
AFTER INSERT OR UPDATE OR DELETE
ON books
FOR EACH ROW
DECLARE
l_action CHAR(1);
BEGIN
IF :NEW.isbn != :OLD.isbn THEN
exc.myraise(exc.cannot_change_unique_id_cd);
END IF;
IF INSERTING THEN
l_action := 'I';
ELSIF UPDATING THEN
l_action := 'U';
ELSIF DELETING THEN
l_action := 'D';
END IF;
INSERT INTO books_hist
(isbn, action, datestamp, oracle_user, real_user,
old_title, old_summary, old_author,old_date_published,old_page_count,
new_title, new_summary, new_author, new_date_published,new_page_count)
VALUES(:NEW.isbn, l_action, SYSDATE, USER, NULL,:OLD.title, :OLD.summary,
:OLD.author,:OLD.date_published, :OLD.page_count,:NEW.title,
:NEW.summary, :NEW.author,36 :NEW.date_published,:NEW.page_count);
END;
Eventi dei Trigger
I trigger quindi entrano in azione Prima o Dopo (BEFORE o AFTER)
uno o più dei seguenti eventi che si possono verificare INSERT,
DELETE, UPDATE. L’evento può essere relativo a un’intera tabella o a
una o più colonne.
UPDATE OF column1_name OR UPDATE OF column2_name ON Tabella
Inoltre il trigger può essere applicato per ogni riga o al verificarsi di una condizione WHEN
[ FOR EACH ROW [ WHEN condition ] ]
Data Dictionary
USER_SOURCE
SQL> describe user_source
Nome Nullo? Tipe
----------------------------------------- --------
----------------------------
NAME
VARCHAR2(30)
TYPE
VARCHAR2(12)
LINE NUMBER
TEXT
VARCHAR2(4000)
Data Dictionary
SQL> describe user_triggers
Nome Nullo? Tipe
----------------------------------------- -------- ----------------------------
TRIGGER_NAME VARCHAR2(30)
TRIGGER_TYPE VARCHAR2(16)
TRIGGERING_EVENT VARCHAR2(227)
TABLE_OWNER VARCHAR2(30)
BASE_OBJECT_TYPE VARCHAR2(16)
TABLE_NAME VARCHAR2(30)
COLUMN_NAME VARCHAR2(4000)
REFERENCING_NAMES VARCHAR2(128)
WHEN_CLAUSE VARCHAR2(4000)
STATUS VARCHAR2(8)
DESCRIPTION VARCHAR2(4000)
ACTION_TYPE VARCHAR2(11)
TRIGGER_BODY LONG
Data Dictionary
SQL> describe user_triggers
Nome Nullo? Tipe
----------------------------------------- -------- ----------------------------
TRIGGER_NAME VARCHAR2(30)
TRIGGER_TYPE VARCHAR2(16)
TRIGGERING_EVENT VARCHAR2(227)
TABLE_OWNER VARCHAR2(30)
BASE_OBJECT_TYPE VARCHAR2(16)
TABLE_NAME VARCHAR2(30)
COLUMN_NAME VARCHAR2(4000)
REFERENCING_NAMES VARCHAR2(128)
WHEN_CLAUSE VARCHAR2(4000)
STATUS VARCHAR2(8)
DESCRIPTION VARCHAR2(4000)
ACTION_TYPE VARCHAR2(11)
TRIGGER_BODY LONG

More Related Content

Similar to Oracle PLSql 4

Intro to SQL by Google's Software Engineer
Intro to SQL by Google's Software EngineerIntro to SQL by Google's Software Engineer
Intro to SQL by Google's Software Engineer
Product School
 
Triggers
TriggersTriggers
Triggers
Pooja Dixit
 
Oracle trigger
Oracle triggerOracle trigger
Oracle trigger
nasrul28
 
Oracle eCommerce (ATG) Database Best Practices
Oracle eCommerce (ATG) Database  Best Practices Oracle eCommerce (ATG) Database  Best Practices
Oracle eCommerce (ATG) Database Best Practices
Kate Semizhon
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
FilestreamFilestream
 

Similar to Oracle PLSql 4 (6)

Intro to SQL by Google's Software Engineer
Intro to SQL by Google's Software EngineerIntro to SQL by Google's Software Engineer
Intro to SQL by Google's Software Engineer
 
Triggers
TriggersTriggers
Triggers
 
Oracle trigger
Oracle triggerOracle trigger
Oracle trigger
 
Oracle eCommerce (ATG) Database Best Practices
Oracle eCommerce (ATG) Database  Best Practices Oracle eCommerce (ATG) Database  Best Practices
Oracle eCommerce (ATG) Database Best Practices
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 

More from Sergio Ronchi

Java lezione 19
Java lezione 19Java lezione 19
Java lezione 19
Sergio Ronchi
 
Java lezione 18
Java lezione 18Java lezione 18
Java lezione 18
Sergio Ronchi
 
Java lezione 17
Java lezione 17Java lezione 17
Java lezione 17
Sergio Ronchi
 
Java lezione 16
Java lezione 16Java lezione 16
Java lezione 16
Sergio Ronchi
 
Java lezione 15
Java lezione 15Java lezione 15
Java lezione 15
Sergio Ronchi
 
Java lezione 14
Java lezione 14Java lezione 14
Java lezione 14
Sergio Ronchi
 
Java lezione 13
Java lezione 13Java lezione 13
Java lezione 13
Sergio Ronchi
 
Java lezione 12
Java lezione 12Java lezione 12
Java lezione 12
Sergio Ronchi
 
Java lezione 11
Java lezione 11Java lezione 11
Java lezione 11
Sergio Ronchi
 
Java lezione 10
Java lezione 10Java lezione 10
Java lezione 10
Sergio Ronchi
 
Java lezione 9
Java lezione 9Java lezione 9
Java lezione 9
Sergio Ronchi
 
Java lezione 8
Java lezione 8Java lezione 8
Java lezione 8
Sergio Ronchi
 
Java lezione 7
Java lezione 7Java lezione 7
Java lezione 7
Sergio Ronchi
 
Java lezione 6
Java lezione 6Java lezione 6
Java lezione 6
Sergio Ronchi
 
Java lezione 5
Java lezione 5Java lezione 5
Java lezione 5
Sergio Ronchi
 
Java lezione 4
Java lezione 4Java lezione 4
Java lezione 4
Sergio Ronchi
 
Java lezione 3
Java lezione 3Java lezione 3
Java lezione 3
Sergio Ronchi
 
Java lezione 2
Java lezione 2Java lezione 2
Java lezione 2
Sergio Ronchi
 
Java introduzione
Java introduzioneJava introduzione
Java introduzione
Sergio Ronchi
 
Java Lezione 1
Java Lezione 1Java Lezione 1
Java Lezione 1
Sergio Ronchi
 

More from Sergio Ronchi (20)

Java lezione 19
Java lezione 19Java lezione 19
Java lezione 19
 
Java lezione 18
Java lezione 18Java lezione 18
Java lezione 18
 
Java lezione 17
Java lezione 17Java lezione 17
Java lezione 17
 
Java lezione 16
Java lezione 16Java lezione 16
Java lezione 16
 
Java lezione 15
Java lezione 15Java lezione 15
Java lezione 15
 
Java lezione 14
Java lezione 14Java lezione 14
Java lezione 14
 
Java lezione 13
Java lezione 13Java lezione 13
Java lezione 13
 
Java lezione 12
Java lezione 12Java lezione 12
Java lezione 12
 
Java lezione 11
Java lezione 11Java lezione 11
Java lezione 11
 
Java lezione 10
Java lezione 10Java lezione 10
Java lezione 10
 
Java lezione 9
Java lezione 9Java lezione 9
Java lezione 9
 
Java lezione 8
Java lezione 8Java lezione 8
Java lezione 8
 
Java lezione 7
Java lezione 7Java lezione 7
Java lezione 7
 
Java lezione 6
Java lezione 6Java lezione 6
Java lezione 6
 
Java lezione 5
Java lezione 5Java lezione 5
Java lezione 5
 
Java lezione 4
Java lezione 4Java lezione 4
Java lezione 4
 
Java lezione 3
Java lezione 3Java lezione 3
Java lezione 3
 
Java lezione 2
Java lezione 2Java lezione 2
Java lezione 2
 
Java introduzione
Java introduzioneJava introduzione
Java introduzione
 
Java Lezione 1
Java Lezione 1Java Lezione 1
Java Lezione 1
 

Recently uploaded

Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 

Recently uploaded (20)

Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 

Oracle PLSql 4

  • 1. PL/SQL – Triggers I Trigger non sono sottoprogrammi invocati esplicitamente, ma vengono eseguiti in seguito al verificarsi di eventi.
  • 2. Esempio Si supponga di voler tenere traccia dei cambiamenti nella tabella books e di avere una tabella book_hist: CREATE TABLE books_hist ( isbn VARCHAR2(13) NOT NULL, action CHAR(1) NOT NULL, datestamp DATE NOT NULL, oracle_user VARCHAR2(30) NOT NULL, real_user VARCHAR2(60), old_title VARCHAR2(200), old_summary VARCHAR2(2000), old_author VARCHAR2(200), old_date_published DATE, old_page_count NUMBER, new_title VARCHAR2(200), new_summary VARCHAR2(2000), new_author VARCHAR2(200), new_date_published DATE, new_page_count NUMBER, CONSTRAINT action_ck CHECK (action IN ('I', 'D', 'U')));
  • 3. Esempio TriggerCREATE OR REPLACE TRIGGER book_hist_trg AFTER INSERT OR UPDATE OR DELETE ON books FOR EACH ROW DECLARE l_action CHAR(1); BEGIN IF :NEW.isbn != :OLD.isbn THEN exc.myraise(exc.cannot_change_unique_id_cd); END IF; IF INSERTING THEN l_action := 'I'; ELSIF UPDATING THEN l_action := 'U'; ELSIF DELETING THEN l_action := 'D'; END IF; INSERT INTO books_hist (isbn, action, datestamp, oracle_user, real_user, old_title, old_summary, old_author,old_date_published,old_page_count, new_title, new_summary, new_author, new_date_published,new_page_count) VALUES(:NEW.isbn, l_action, SYSDATE, USER, NULL,:OLD.title, :OLD.summary, :OLD.author,:OLD.date_published, :OLD.page_count,:NEW.title, :NEW.summary, :NEW.author,36 :NEW.date_published,:NEW.page_count); END;
  • 4. Eventi dei Trigger I trigger quindi entrano in azione Prima o Dopo (BEFORE o AFTER) uno o più dei seguenti eventi che si possono verificare INSERT, DELETE, UPDATE. L’evento può essere relativo a un’intera tabella o a una o più colonne. UPDATE OF column1_name OR UPDATE OF column2_name ON Tabella Inoltre il trigger può essere applicato per ogni riga o al verificarsi di una condizione WHEN [ FOR EACH ROW [ WHEN condition ] ]
  • 5. Data Dictionary USER_SOURCE SQL> describe user_source Nome Nullo? Tipe ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(30) TYPE VARCHAR2(12) LINE NUMBER TEXT VARCHAR2(4000)
  • 6. Data Dictionary SQL> describe user_triggers Nome Nullo? Tipe ----------------------------------------- -------- ---------------------------- TRIGGER_NAME VARCHAR2(30) TRIGGER_TYPE VARCHAR2(16) TRIGGERING_EVENT VARCHAR2(227) TABLE_OWNER VARCHAR2(30) BASE_OBJECT_TYPE VARCHAR2(16) TABLE_NAME VARCHAR2(30) COLUMN_NAME VARCHAR2(4000) REFERENCING_NAMES VARCHAR2(128) WHEN_CLAUSE VARCHAR2(4000) STATUS VARCHAR2(8) DESCRIPTION VARCHAR2(4000) ACTION_TYPE VARCHAR2(11) TRIGGER_BODY LONG
  • 7. Data Dictionary SQL> describe user_triggers Nome Nullo? Tipe ----------------------------------------- -------- ---------------------------- TRIGGER_NAME VARCHAR2(30) TRIGGER_TYPE VARCHAR2(16) TRIGGERING_EVENT VARCHAR2(227) TABLE_OWNER VARCHAR2(30) BASE_OBJECT_TYPE VARCHAR2(16) TABLE_NAME VARCHAR2(30) COLUMN_NAME VARCHAR2(4000) REFERENCING_NAMES VARCHAR2(128) WHEN_CLAUSE VARCHAR2(4000) STATUS VARCHAR2(8) DESCRIPTION VARCHAR2(4000) ACTION_TYPE VARCHAR2(11) TRIGGER_BODY LONG