RDBMS
RDBMS
PL/SQL engine
Features of PL/SQL
1. BLOCK structure :
2. Variables and constants :
3. Control structure :
4. Exception haldling :
5. Modularity
6. Cursors:
7. Build in function
Data types:
Storage format
1)CHAR DATA
TYPE:char,nchar,nvarchar,varchar2(size),long ,raw,l
ong raw
2)Numeric data type: number(p,s) i.e number(3,2)
output: 234.25,numeric(p,s),double
precision ,float,dec(p,s)/demicial(p,s) 38 digit ,int
integer ,real 63 decimal digits:
Urowid(size):universal ROWID
Pl/SQL
Declaration
Variable declaration;
Begin
Program Excuation;
Exception
Exception handling;
End;
Function:
It take one or more value and return single result .
Build in function
1)ABS();
sql> select abs(-15) from dual;
ouput
15
2)Mod()
Sql> select MoD(15,4) from dual;
Output
3
3)SQRT()
Sql> select SQRT(121) from dual;
Output
11
4)ASCII()
Select ASCII(‘A’) from dual;
Output
65
5)INITCAP()
Select INITCAP(‘how are you’) from dual;
Output
How Are You
________________________________________________________
_____
Function
________________________________________________________
________________________________________________________
________
Emp Ffunction
Empno sal
f_no f_sal
Calling function
Delete a function
Drop function function_name;
Drop function Ffunction;
Declare
V_mon varchar2(10);
R number;
Begin
V_mon:=&v_mon;
R:= f3(v_mon)
If (r>0) then
D_o.P_l(‘no of emp joined in || v_mon||’=’||r);
Else
D_o.P_l(‘no of emp joined in || v_mon);
End if
End;
Procedure
Syntax
Execute a procedure
SQL> execute myproc1(77780);
Delete a procedure
Drop procedure procedure_name;
Eg:
Drop procedure myproc1;
Emp p1
Deptno sal p_deptno max_sal
Create or replace procedure p1(p_deptno in number)
As
Max_sal emp.sal%type;
Begin
Select max(sal) into max_sal from emp where
depno=p_deptno;
If (max_sal>0) then
D_O.P_L(‘max sal =’||max_sal);
Else
D_O.P_L(‘deptno does not exist’);
Enf if;
End p1;
SQL> execute p1(90);
________________________________________________________
____
Calling function
Delcare
V_deptno emp.deptno%type;
Begin
V_deptno:=&v_deptno;
P1(v_deptno);
End;
/
Cursor
A cursor is a temporary work area created by system
memory when SQl and PL/SqL statements are executed
.
Attributes
%rowcount : no rows processed by sql statement .
%found :true ,if at least one row processed.
%notfound: true no rows processed
%isopen: true cursor open
False cursor close
Declaration
Syntax:
Cursor cursor_name [(parameters)] [return return_type]
is select quesry
Eg:
Declare
Cursor c_deptno is select * from emp;
Open a cursor
Open cursor_name[parameters];
Eg:
Declare
Cursor c_deptno is select * from emp;
Begin
Open c_deptno;
End;
End;
Closing cursor
Eg: close c_deptno;
For ……..loop using cursor
Eg
Declare
Cursor c_deptno is select ename,sal,deptno from emp;
Begin
Open c_deptno
For i in c_deptno
Loop
D_O.P_L(i.ename||i.sal||i.deptno);
End loop
End;
Types of cursor
1)Implicit
2)Explicit
3)Parameterised
1)Implicit cursor:
These are created by default when DML statement
like insert update and delete statement are
executed and also select statement
Eg:
Prog1: print number of rows deleted from emp
Declare
Print_row_deleted number;
Begin
Delete from emp;
Print_row_deleted=SQL%rowcount ;
Dbms_output.put_line(‘no of rows deleted’||’ ‘||
Print_row_deleted);
End;
SQL> /
No of rows deleted 12
PL/SQL procedure successfully completed
Explicit cursor
Syntax:
Parameterised cursor
Eg:
Accept a salary and print name salary and jon of emp
having salary less than equal to accepted salary
Declare
Cursor c1(c_sal emp.sal%type) is select name,sal,job
from emp where sal<=c_sal;
Emp_rec c1%rowtype;
V_sal emp.sal%type;
Begin
V_sal=&v_sal;
For emp_rec in c1(v_sal)
Loop
D_O.P_l(emp_rec.name||emp_rec.sal||emp_rec.job);
End loop;
End;
SQL>/
Trigger
A trigger is triggered automatically when as
associated DML statement is executed.
Or
A trigger is a PL/SQL block structure which fired
when a DML statement like insert delete and
update command is excuted on a database
table .
How to create a trigger?
Program 1:
Consider the following relationship database
Doctor(dno,dname,dcity)
Hospital(hno,hname,hcity)
Doc_hosp(dno,hno)
__________________________________________________
___
Write a function to return the count of number of
hospital located in pune city.
ACID properties
___________________________________________________
_____
A=atomicity
C= consistency
I= isolation
D=durability
ACID properties
Ti :
read(A)
A:= A-50;
Write(A);
Read(B);
B:= B+50;
Write(B);
Transaction State
Serializability :
1) Conflict serializability
2)View serializability
1)Conflict serializability :
T1 and T2 are two transaction and
S is schedule
Ii and Ij are two instruction .
If Ii and Ij refer to different data item
Then
Ti and Ijcan be executed in any sequence.
Conflict pairs
R --- W
W -- R
W-- W
T1 T2
Read (A)
Write(A)
Read(A)
Write(A)
Read(B)
Write(B)
Read(B)
Write(B)
Precedence Graph:
No cycle /loop
Conflict serializable
Serial
Consistent
Sequence : T - T2
View Serializable :
S1:
T2 T3
T1
R(A
)
W(A)
W(A
)
W(A
)
Precedence graph:
Non conflict serializable
If we get loop in the graph then use view serializability
S2
T1 T2 T3
R(A)
W(A)
W(A)
W(A)
S1 S2
T1 T2 T3 T1 T2 T3
R(A) R(A)
W(A) W(A)
W(A) W(A)
W(A W(A)
)
A=100 A=100
A=a-40 A=a-40
A=60 A=60
A=a-40 A=a-40
A=20 A=60-
40
A=a-20
A=20
A=0
A=a-20
A=0
Indegree
T1=0
T2=1
T3=2
Therefore Sequence: : T1 - T2 - T3
________________________________________________________
_____Recoverability
Recoverability refers to the ability of system to restore
its state to a consistent and valid condition after a
failure or crash .
Keys:
1)Atomicity: Ensure that transactions are treated as a
single transaction, indivisible unit of work .if
transaction fails the system roll back to its previous
state .
2)Consistency: verifies that the database remains in
consistent state, even after multiple transaction
have been applied.
3)Durability:Grarantees that once a transaction has
been committed ,its effects are permeant and
survive even in the events of a system failure .
Types of Recoverability
1)Transaction: restored the database to a consistent
state by rolling back transactions that wre
interrupted or fail.
Eg:
T1 T2
Read(A)
Write(A)
Read(A)
Read(B)
Cascadeless Schedule:
It ensure that the rollback of one transaction doe not
cause rollback of other transactions
Properties:
1)No dirty reads:
2)Strict scheduling :
T1 T2 T3
Read(A)
Read(B)
Write(A)
Read(A)
Write (A)
Read(A)
________________________________________________________
_____
Solution:
S1:
T1 T2
Read(X)
X=X+100
Write(X)
Read(Z)
Read(X)
X=X-Z
Write(X)
Read(Y)
Read(Z)
Y=Y+Z
Write(Y)
Read(Y)
Y=Y-100
Write(Y)
S2:
T1 T2
Read(X)
X=X+100
Write(X)
Read(Z)
Read(Y)
Read(Z)
Read(X)
X=X-Z
Write(X)
Y=Y+Z
Write(Y)
Read(Y)
Y=Y-100
Write(Y)
Solution:
S1:
T1 T2
Read(X)
Read(a)
X=X+a
Write(X)
Read(Y)
Read(a)
Y=Y+a
Write(Y)
Read(Y)
Y=Y+a
Write(Y)
S2
T1 T2
Read(X)
Read(a)
Read(Y)
Read(a)
X=X+a
Write(X)
Y=Y+a
Write(Y)
Read(Y)
Y=Y+a
Write(Y)
Chap 3:
E_Commerce
Short question
i)What is Electronic Data Interchange (EDI)?
ii) Define B2B e-commerce.
iii) Name one legal requirement for e-commerce.
iv) State one advantage of e-commerce for
consumers.
v) What is the final stage of the e-commerce
adoption life cycle?
vi) Give an example of a payment gateway.
vii) What is spoofing in the context of e-
commerce security?
i) Define E-commerce.
ii) What is meant by B2C in e-commerce?
iii) Name one essential component of the e-
commerce infrastructure.
iv) State one function of e-commerce.
v) What is the first stage of the e-commerce
adoption life cycle?
vi) Give an example of an electronic payment
system.
vii) What is phishing in the context of e-
commerce security?
Long Questions
________________________________________________________
____
Short note
i)The internet and E-commerce.
ii) A value exchange system in e-commerce
iii) Electronic Funds Transfer (EFT).
iv) Modern Payment Cash
v) The Threats posed by malicious code.
vi) Denial of Service (DoS) attacks
i) World Wide Web in the growth of e-commerce.
ii) Positioning Payment in e-commerce.
iii) Distributed Denial of Service (DDoS) attack .
iv) Stealing Credit card information
v) security policy
vi) Problems with online cash
Compatibility Function
S X
S True False
X False False
Locking Technique
1.Binary locking : 1(locked) or 0 (unlocked)
2.Two-phase locking :locked _-- unlocked
Two-phase locking
1.Growing phase :
Ti can obtain lock
But can not release it
2.Shrinking phase:
Ti can release lock
But can not obtain new lock
Eg:
lock_X(B);
Read(B)
B:=B-50;
Write(B);
lock_S(A)
Read(A)
Lock_X(A)
Read(A)
A:=A+50
Write(A);
Unlock(B)
Unlock(A);
Lock_S(B)
Read(B)
Display(A+B)
Unlock(A)
unlock(B)
.