bank project and pl (1).docx

Upload: abhisek-ray

Post on 06-Mar-2016

15 views

Category:

Documents


1 download

TRANSCRIPT

BANK PROJECT AND PL/SQL

KCB_ACC_TAB----------------------

1 create table kcb_acc_tab 2 ( 3 accno number primary key, 4 name varchar2(20) constraint name_nn not null, 5 actype char check(actype in('s','c','fd')), 6 doo date default sysdate, 7 bal number(8,2) not null 8* )QL> /

Table created.

QL> insert into kcb_acc_tab values(37002167543,'srinivas','s',sysdate,15000) 2 /

row created.

QL> commit 2 /

commit complete.

KCB_TRAN_TAB-------------------------

create table kcb_tran_tab(tid number,accno number(20) references kcb_acc_tab(accno),trtype char(10) check(trtype in('d','w')),dot date default sysdate,amt number(7,2) check(amt>100))

SEQUENCE-----------------create sequence s1start with 1increment by 1maxvalue 1000minvalue 0nocachenocycle

1) Write a PL/SQL program to modify the balance after deposite the amt and insert the transaction details also.

declarei kcb_acc_tab%rowtype;k kcb_tran_tab%rowtype;begini.accno:=&accno;k.trtype:='&trtype';k.amt:=&amount;select bal into i.bal from kcb_acc_tabwhere accno=i.accno;if k.trtype='D' theni.bal:=i.bal+k.amt;end if;update kcb_acc_tab set bal=i.bal where accno=i.accno;insert into kcb_tran_tab values(s1.nextval,i.accno,k.trtype,sysdate,k.amt);commit;end;

2) write a PL/SQL program for enter the transaction details perform the validationi)if it is deposite update the bal and insert the transaction detailsii) if it is withdraw before withdrawcheck the current bal if validationcontrol satisfy then onlyperform the withdraw

declarei kcb_acc_tab%rowtype;k kcb_tran_tab%rowtype;begini.accno:=&accno;k.trtype:='&trtype';k.amt:=&amt;select actype,bal into i.atype,i.balance from kcb_acc_tab where accno=i.accno;if k.trtype='D' theni.bal:=i.bal+k.amt;elsei.bal:=i.bal-k.amt;if i.actype='s' and i.baltype is Ref cursor [return % rowtype]

note: Explicit cursor is a static cursor Explicit cursor refers always one work area associated with the cursor. features of cursors: cursor with parameter for update as { of column name now wait } ---> where current of ---> row id ---> sub queries

parametric cursor: A cursor define with parameter is called parametric cursor. the default mode of parameter is " in ".

syntax: cursor (parameter name dtype,---) is select stmt;

where current of clause to refuse the current record and fetched from the explicit cursor.for update clause explicitely focus the records stored in the private work area.

Composite data types (collections) : These are two types which cannot hold any data physically i) PL/SQL Record ii) PL/SQL Table i) PL/SQL Record: A PL/SQL Record is allows you to treat sevaral variables as a unit. PL/SQL Records are similar to strutures in Csyntax: Type is record (field name1, [field name2]---); ii) PL/SQL Table: It holds the elements of the similar datatypes. it is similar like a array concept in Csyntax: type is table of index by binary-integer;here table name is collection namedata type---> what type of data can plce in a indexindex by---> perform the no.of indexes which can hold the data temporarlybinary-integer---> it is a system datatype

Exception: An exception in pl/sql block is raised during exception of block.---> it terminates the main body of the action means a block always terminates when pl/sql raised an exception.---> if the exception is handled then the pl/sql block terminates successfully.

Types of Exceptions: Raised implicity: (predefined exceptions) The exceptions which are declared by the oracle and raised implicity these exceptions are called Raised implicity.Raised explicity: Defined by the user handle by the user according to the requirement.

Predefined exception list:

Exception Name Exception Descriptioni) Dup_Val_On_Index Ora_0001 Unique constraint violatedii) Invalid cursor Ora_1001 Illigal cursor operationiii) Login_Denied Ora_01017 Incase of invalid username and passwordiv) No_Data_Found Ora_01403 Data is not exist in the tablev) To_Many_Rows Ora_1422 A select ---into stmt matches more than one rowvi) Invalid_Number ora_1722 Conversion to a number failed

syntax: Exception When Then Sql stmt When Then Sql stmt When Others Then Sql stmt Raise Application Error: It is built in function It is used in pl/sqlThis enable due to specified user defined error message and suspend the task.syntax: Raise_Application_Error ; Error num limit is(-20,000 and -20,999)

User Defined Exception: The exception which is defined by user called as a User Defined Exception. This exception can invoked by the user by raised stmt.steps:i) declare the exception (in declare function)ii) raise in executable section (explicitly using raise stmt)iii) handled the raised exception

Sql Code: It holds the currently raised error numbersSql Error: It holds the currently raised error msgssyntax: display (Sql Code || Sql Error);

Prgama Exception-init: I need to display my our message,---> it is used to handle the error just like a exception---> to associate exception in the error code the pragma exception init is defected.---> it contains two parameters i) exception name ii) error number(or)code

Sub PrgramsA set of PL/SQL stmts with a name stored permentely in database CalledTwo types:ProceduresFunctions

Procedure:A Procedure is a Named PL/SQL Block performing one or more actions associated to business logic.It is also called Stored procedureIt accept the parametersIt is Explicitely called by using Executives.It may or may not Return values.Procedurre cannot use With Select Statement.

Advantages of Procedure:

Using Procedure a programmer to shift the code from the Application side to the database side.Business Rules and Application logic to be stored As Procedures with in the database side.The procedural code stored within the database can beReused Anywhere with in the Application.They nednot be rewritten in each code module of the aplicationso saves the creation time of the application.Reduce the maintaince effort.

Types of Procedures:

Stand alone Procedure:The procedure which is created outside of the package called asThis procedure will stored in user_procedures.Part of the package:This Procedure is define in a package This procedure is not stored Anywhere.

Parts of the Procedure:Specification: nothing but declaration sectionhere we can give the procedure nameparameters and local variables.

Body: it contains pl/sql code based on this PL/SQL only the Action of procedure depend.

Syntax:Create or replace Procedure[parameter[mode]datatype,-----)] is/as[local variable]Begin;Exception;End[procedure name];

Programs:Write a pl/sql procedure to display the SI AND CI

>set serveroutput oncreate or replace procedure simintr(p in number,n in numbr,r in number)issi number;ci number;beginsi:=(p*n*r)/100;ci:=p*power(1+r/100),n);dbms_output.put_line('The si is '|| si); ('The ci is'||ci);End simintr;syntax:execute;Eg;execute simintr(100,10,12);

PL/SQL:Beginprocedure name;End;

Ex:Begin simintr(100,10,12);end;

o/p:si is=ci is=

Parameter modes:Three types of modesi)IN ii) OUT iii) INOUT

IN:It accepts a default typeAlways Accept values into subpgmsIt cannot be assigne with values in subpgms

Out:It returns a value from subpgminitilizes in subpgmit cannot carries a value into a subpgmInout:carries a value into a subpgmReturn a value from The subpgmMax 32 Argments into subpgm

Syntax: [parameter mode]

note: Procedure maynot Return a value but through the outmode parameter procedure may Returned

Remove the procedure:Drop procedure;

Eg:create or replace procedure sp(m in number, n out number)isbeginn:=m*m;end sp;

using Bind variablevar r numberexec sp(10,:r);procedure createdprint :r

create or replace procedure proc_cal(m in number,sr out number,cr out number)isbeginsr:=m*mcr:=m*m*m;end proc_cal;

var sr numbervar cr numberexec proc_cal(10,:sr,:cr)print :srPrint :cr

Call this procedure in another PL/SQL BlockDeclaresr number;cr number;beginproc_cal(10,sr,cr);dbms_output.put_line('the sr is:'||sr); ('the cr is:'cr);end;

o/p: sr is:cr is:

Remove the procedure:Drop procedure ;

Pragma _autonomous transaction:It is used write the TCL commonds in trigger.it is used also in nested procedure to make each procedure independently for TCL.

Function:The function is a PL/SQL Block that Returns a Single value.

Function can accept one or more parameters or no parameters.function must hava a Return clause in the executable section of a function.it is called part of expression.it is used for calculation of a value.function can user with the select stmt.function cannot use with select stme when it has a outmode,inout mode parameters.

Syntax:create or replace function (parameter name[mode] datatype,------)Return is/as[local variable]Begin;Return ;Exception;end ;

Execution:select (argments) from dual;pl/sql: begin ; end;

Packages:It's group of logically Related PL/SQL Types Objectsand Subprograms.

Package can not be invoked,parameter and nested.

It has usually Two partsi)specificationII)Body

It allowa the oracle server to read multiple objects into memory at once.

means when we call packaged PL/SQL construct for the first time,the wholepackage is loaded into memory.

later calls to construct in the same package Require no disk I/O

Syntax for specification:create or replace packbodyis/as[public variable declaration];

End ;

Syntax for The Body:create or Replace packBody is/as[private Variable declaration];

End [pack name];

Execution: Execute.(parameter values);

Triggers:A set of PL/SQL stmts automatically executed whenever an DML stmts is performed on table. OrA Database Trigger is a named PL/SQL Block stored in a database and Executed Implicitely whenTriggering Event occurs.

A Triggering Event is a Dml stmt Associated with "insert","update" or "delete" stmtexecuted againest a database Table.

Advantages of Triggers:

It is used to implement user defined Restriction on table.

used to impose business Rules on data.Provides high security.

Trigger can fire before or after a Triggering Events.

Before: By using Before we can make the Trigger action Before performing Transaction.

After: By using After we can execute the Trigger after perform a Transaction.

Defined Trigger :

Row Trigger: This Trigger will be fired for every row manipulated. Generaly used for data auditing applications.By using FOR EACHROW u can make the trigger as Row level.

Statement Triggers: (table level)

The Trigger will be fired only once for Dml stmt.

Types of Triggers:i) Application levelII) Database Triggers.

Application level Triggers:The validation or rules different from each and every application we should use the application trigger.

Eg: D2K forms and Reports:

Database Triggers:The validation or Rules are common for Every Applicant who are Applicant who are accessing theDatabase we can use the Database Triggers.Trigger Qulifiers::New.:Old.

Triggers Qulifiers can use only in Row level.

Syntax:Create or Replace Trigger After/Before (of )on [for eachrow](when )[declare local variable];BeginSql stmts----------------------Exception Executable stmtsEnd [Trigger name];Difference between Procedure and Triggers.

Procedure Triggeri)call explicitely i)call implicitely against the eventsII) accept the parameters ii) triggers not accept parametersiii)Procedure can nested iii)trigger cannot nestediv)procedure can used for iv) triggers cannot usedstoring the images

V) in procedure u can use V)Trigger may not give the the compilation error but gives the Run time errothe TCL command w/o compileand Runtime errors

Fragma autonoums is optional Fragma autonomous is mandatory

Difference Beteen Triggers and Constraints. Triggers Constraints

We can create the user define constraints are the building programsTriggers and we can change the and we cannot change the behavior of the constraints.behavior of this trigger.

Trigger can check only it will check the existing data andfuture data. feture data. we can disable the all if may not disable all the constraints in a table at once.the triggers on a tableat a time.