0% found this document useful (0 votes)
3 views

Advanced SQL Lec2

The document provides an overview of functions, procedures, and triggers in SQL. It describes their syntax, usage, and differences, including how functions can return values while procedures may or may not. Additionally, it explains triggers as automatic database objects that execute on specific events like insert, update, or delete, along with their advantages and structure.

Uploaded by

udaymodel31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Advanced SQL Lec2

The document provides an overview of functions, procedures, and triggers in SQL. It describes their syntax, usage, and differences, including how functions can return values while procedures may or may not. Additionally, it explains triggers as automatic database objects that execute on specific events like insert, update, or delete, along with their advantages and structure.

Uploaded by

udaymodel31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Functions , Procedures

Triggers
FUNCTIONS
• Functions and procedures allow “business logic”
to be stored in the database and executed from SQL
statements.

• These can be defined either by the procedural


component of SQL or by an external programming
language such as Java, C, or C++.
FUNCTIONS
SYNTAX:
create [or replace]
function <function name> (<arg list>) return data type
Is
............. ---------> Declaration part
Begin
............. ---------->Execution part
exception
............-------------->Exception handling part
return <value>;
end;
/
FUNCTIONS
Program: Given string is palindrome are not using functions
create or replace function funpalin(str varchar) return varchar
is
str1 varchar2(15);
begin
for i in reverse 1..length(str)
loop
str1:=str1||substr(str,i,1);
end loop;
return str1;
end;
/
FUNCTIONS
declare
str varchar2(15):='&str';
r varchar2(15);
begin
r:=funpalin(str);
if (str=r) then
dbms_output.put_line(' Given string is palindrome');
else
dbms_output.put_line('Given string is not a palindrome');
end if;
end;
/
PROCEDURES
• Procedures are used to perform a specific task and may
or may not return a value.
• Syntax:
create [or replace]
procedure <procedure name> (<arg list>)
is
............. ---------> Declaration part
begin
............. ---------->Execution part
exception
............ ---------->Exception handling part
end;
PROCEDURES
create or replace
procedure fact2(n in number, m out number)
is
fact number:=1;
begin
for i in 1..n
loop
fact:=fact*i;
end loop;
m:=fact;
end;
PROCEDURES
Types of parameters used in procedures

1. IN parameter:
• This parameter is the default parameter and it
is same as a constant.
• Initialization is must to parameter and value
of this parameter is unchanged through out
the execution of the program .
i.e.,it remains the same.
PROCEDURES
2.OUT parameter:

• This parameter is exactly same as the uninitialized


variable initially , but during execution of the
program , the program assigns value to this
parameter and this value is sent back to the calling
environment.

OUT parameter works same as the return value of a


function. Only one time assignment is allowed
through out the execution of the program.
PROCEDURES
3.IN OUT parameter

• This parameter is exactly same as initialized


variable and during the execution of the program,
the program assigns the value returned from the
calling environment to this variable.
TRIGGERS
• A trigger is a database object.

• A trigger is almost same as procedure.

• A procedure always requires an explicit call for


execution but Triggers are executed automatically
when any triggering event occurs.

• Insert , Delete , Update are considered as the


triggering events of the database trigger.

• These events initiate the firing of trigger.


TRIGGERS
Advantages:
1.A database trigger is a security object to provide
security to the table like tracking the transaction.

2.A database trigger is used to define the complex


business constraints.

3.A database trigger is also used to define the default


values of database table.
TRIGGERS

Parts of a trigger:

1.Trigger event

2.Trigger type

3.Trigger restriction
TRIGGERS

1.Trigger event:

• The trigger event is the time, when the trigger must


be implicitly executed .

• There are three trigger events usually used in a


trigger definition. i.e., insert/update/delete.
TRIGGERS
2. Trigger types:
(i) Before trigger may be defined to execute a trigger,
before trigger event fires.
(ii) After trigger executes after the trigger event.
(iii)for each row type of trigger executes everytime a
record is deleted, updated and inserted.
For example,
"for each row trigger" executes 10 times when 10
records are updated by an sql query.
(iv) for each statement trigger may be executed only
once for any sql statement.
TRIGGERS

3.Trigger restriction:

• It is the when clause added with the trigger


definition . It is an additional event to the trigger
definition.
TRIGGERS
create or replace
trigger <trigger name>
before/after [insert][or update] [or delete]
on <table name>
[for each row]
when (<condition>)
declare
----
begin
---
exception
----
end;
TRIGGERS
• create table student77
( rno number(5),
sname varchar2(15),
course varchar2(4),
sem number(2));

• insert into student77 values (&rno, '&sname', '&course',&sem);


• create table student_backup
( rno number(5),
sname varchar2(15),
course varchar2(4),
sem number(2));
TRIGGERS
• Write a trigger to backup a record or multiple
records are deleted from the student77 table.
create or replace trigger trig1
before delete on student77
for each row
begin
insert into student_backup values
(:old.rno, :old.sname, :old.course, :old.sem);
end;
/
THANK YOU

ramesh.chejarla@gmail.com 9959597459

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy