You are on page 1of 34

UNIVERSITY OF MUMBAI

INSTITUTE OF DISTANCE AND OPEN LEARNING (IDOL)*


*(Institute of Distance Education (IDE) is renamed)

CERTIFICATE
THE EXPERIMENTS DULY SIGNED IN THIS JOURNAL REPRESENT
THE BONAFIDE WORK BY MST BHUSHAN PRADEEP KOLAMBEKAR
IN SEMESTER III OF SECOND YEAR OF MASTER IN
COMPUTER
APPLICATION
(MCA)
IN
THE
COMPUTER
LABORATORY OF PCP CENTER SHREE RAM COLLAGE,
BHANDUP FOR SUBJECT DATABASE MANAGEMENT SYSTEM
DURING ACADEMIC YEAR 2014-2015.

____________________
____________________________
LECTURE IN CHARGE
MCA

HEAD, DEPARTEMENT OF

______________________

EXTERNAL EXAMINER

INDEX
SR. NO.
1
2
3
4
5
6
7
8

TITLE
CREATION OF BANKING ENTERPRISE DATABASE.
CREATON OF EMPLOYEE TABLE
CREATION OF DEPARTMENT TABLE
TYPES OF CURSOR.
CREATION OF FUNCTION.
CREATION OF STORED PROCEDURE.
CREATION OF PACKAGE.
CREATION OF TRIGER.

WRITE A BLOCK THAT ACCEPTS NAME AND DISPLAY IT 5


TIMES IN OUTPUT SCREEN.

10

WRITE A PL/SQL BLOCK THAT ACCEPTS


PRINCIPAL,DURATION(IN MONTHS) AND RATE OF INTEREST
AND CALCULATE MATURITY AMOUNT.

11

WRITE A BLOCK THAT ACCEPTS RADIUS AND CALCULATE


AREA OF CIRCLE. USE PI AS CONSTANT.

12

WRITE A BLOCK THAT ACCEPTS NUMBER AND DISPLAY


WHETHER IT IS LESS THAN, OR EQUAL, OR GREATER THAN
10.

SIGN

Practical No.1

Aim: Creation of banking Enterprise Database


Create table branch
(br_name varchar(15) primary key,
br_city varchar(1O),
asset int)
create table account
(acc_no varchar(1O) primary key,
br_name varchar(15),
balance int,
foeign key(br_name) references branch)
create table depositor
(cust_name varchar(15),
acc_no varchar(1O) foreign key references account,
foreign key(cust_name) references customer)
create table customer
(cust_name varchar(15) primary key,
cust_street varchar(20),
cust_city varchar(1O))
create table borrower
(cust_name varchar(15) foreign key references customer,
Loan_no varchar(15) foreign key references loan)
create table loan
(loan_no varchar(1O) primary key,
br_name varchar(15) foreign key references branch,
account int)
insert into customer values(Adams,Spring,Pittsfield)
insert into customer values(Brooks, Senator, Brooklyn)
insert into customer values(Curry,North,Rey)
insert into customer values(Glenn, Sand Hill,Woodside)

insert into customer values(GIenn,Walnut,Stamford.)


insert into customer values(Hayes,Main,Harrison)
insert into customer values(Johnson,AIma,Stamford)
insert into custOmer values(Jones, Main Harrison)
insert into customer values(Lindsay,Park,Pittsfield)
insert into customer values(Smith,North,Rey)
insert into customer values(Turner,Putnam,Stamford)
insert Into customer values(William5,Nassau,Princeton)
insert into depositor values(Hayes,.A- 102)
insert into depositor values(Johnson,A-lOl)
insert into depositor values(Johnson,A-201)
insert into depositor values(Jones,A-217)
insert into depositor values(Lindsay, A-222)
insert into depositor values(Smith,A-215)
insert into depositor values(Turner,A-305)
insert into branch values(Brighton,Brooklyn,7100000)
insert into branch values(Downtown,Brooklyn,9000000)
insert into branch values(Mianus,Horseneck,400000)
insert into branch values(North Town,Rye,3700000)
insert into branch values(Perryridge,Horseneck, 17000000)
insert into branch values(Pownal,Bennington,300000)
insert into branch values(Redwood,PaIo AIto,2100000)
insert into branch values(Round HiII,Horseneck, 8000000)
select * from branch

insert into account values(A-101,Downtown,500)


insert into account values(A-102,Perryridge,400)
insert into account values(A-102,Brighton,900)
insert into account values(A-215,Mianus,700)
insert into account values(A-217,Brighton,750)
insert into account values(A-222,Redwood,700)
insert into account values(A-305,Round Hill,350)
select * from account
insert into loan values(L-11,Round Hill,900)
insert into loan values(L- 14,Downtown, 1500)
insert into loan values(L-15,Perryridge,1500)

insert into loan values(L-16,Perryridge, 1300)


insert into loan values(L-17,Downtown,lOOO)
insert into loan values(L-23,Redwood,2000)
insert into loan values(L-93,Mianus,500)
select * from loan
insert into borrower values(Adams, L-16)
insert into borrower values(Curry,L-93)
insert into borrower values(Hayes,L-15)
insert into borrower values(Johnson,L-14)
insert into borrower values(Jones,L- 17)
insert into borrower values(Smith,L-11)
insert into borrower values(Smith,L-23)
insert into borrower values(Williams,L-17)
Select * from borrower

1.Find the names of all branches in the loan relation


Select br_name from loan
2. Find all loan numbers for loans made at the Perryridge branch with loan amount greater than
1200
Select l_no from loan where br_name=Perryridge and amount >1200
3,Find the loan number with loan amounts between 90,000 and 100000
Select l_no from loan where amount between 90000 and 100000
4. Find the customer names, loan numbers and loan amounts for all loans at the Perryridge
branch.
Select c_name, b.l_no, amount from borrower b,loan l where b.l_no=l.l_ho and
br_name=Perryridge
5. Find all customers who have a loan from the bank, find their names, loan numbers and loan
amount
Select c_name, t.l_no,s,amount from borrower t, loan s where t.l_no=s.l_no

6. Find the names of all branches that have assests greater than at least one branch located in
Brooklyn.
Select distinct t.br_name from branch t, branch s where t.assests>s.assests and
s.br_city=Brooklyn
7. Find the names of all customers whose street address includes the substring Main
select c_name from customer where c_street like %Main%
8. Find in alphabetic order all customers who have a loan at the Perryridge branch.
Select distinct c_name from borrower b, loan l where b.l_no=l.l_no and br_name=Perryridge
Order by c_name
9. Find all customers having a loan, an account or both at the bank
Select c_name from depositor
Union
Select c_name from borrower
10. Find all customers who have both a loan and an account at the bank.
Select c_name from depositor intersect Select c_name from borrower
11. Find the average account balance at each branch.
Select br_name, avg(bal) from account Group by br_name
12. Find the number of deposits for each branch
Select br_name, count(distinct c_name) from depositor d, account a where d.acc_no=a.acc_no
Group by br_name
13. Find the branch name whose average balance is greater than 1200.
Select br_name, avg(bal) from account Group By br_name Having avg (bal) > 1200
14. Find the average bal for each customer who lives in Harrison and has at least 3 accounts.

Select.d.c_name , avg(bal) from depositer d, account a, customer c where d.acc_no=c.acc_no


and
d.c_name=c.c_name and c.city=Harison Group by d.c_name having count(distinct
d.acc_no)>=3
15. Find the loan numbers in the loan relation with null values for amount.
Selectl_no
From loan
Where amt is null
16.Find those customers who are borrowers from bank.
Select distinct c_name
From borrower
Where c_namein(select c_name From depositor)
17.Find all customers who both an account and a loan at the Perryridge branch
Select distinct c_name
From borrower b,loan 1
Where b.1_no=1.1_no and br_name=Perryridge and (br_name,c_name)in
(select br_name,c_name

From depositor,account a
Where d.cc_no=a.acc_no)
18.Find all customers who do have a loan at the bank,but do not have an account at the
bank
Select distinct c_name
From borrower
Where c_name not in (select c_name From depositor)
19.Find the names of all branches that have assets greater than those of atleast one
branch located in Brooklyn.
Select br_name
From branch
Where assets>some(select assets From branch Where br_city=Brooklyn)

20.Find the branch that has highest average balance


Select br_name
From branch
Group by br_name
Having avg(bal)>some all(select assets from branch where
br_city=Brooklyn)
21.Delete all account tuples in the Perryridge branch
Delete from account Where br_name=Perryridge
22.Delete all loans with loan amount between 1300 and 1500
Delete from account
Where amount between 1300 and 1500
23.Delete ball account tuples at every branch located in Needham
Delete from account
where br_namein(select br_name From branch Where br_city= needham)

Practical No.2

Aim: Creation of Employee Table

Create table Employee ( EmpNo number(4), Ename varchar2(20), desig varcahr2(8), caderno
number(4), Doj date, Salary number(7,3), comm number(7,3), deptno number(2));

1. Make EmpNo as primary key.


Alter table Employee add constraint pk2 primary key (empno);

2. Select all employees who are under the manager disgnation or have salary grater than
10000.
Select EmpNo, Ename, desig, salary from Employee where design =
Manager or salaey > 10000.

3. List all employees whose name begin with v


Select * from Employee where Ename like v%

4. Display all employees with their salary listed in descending order.


Select * from Employee Order by Salary Desc

5. Find the highest & lowest salaries & the difference between them.
Select Max(salary), Min(salary), Max(salary)-Min(salary) from Employee.

6. Give a query to increase the salary of the manager to 10%.


Update Employee set Salary = 1.1 * salary where desig = Manager

7. Find the total salary & total commission of managers.


Select sum(salary), sum(comm) from Employee where desig = Manager

8. Display all employees who have joined on 10 may 2002.


Select * from Employee where doj = 10-may-2002

9. Find the average salary of AGM.


Select avg(salary) from employee where desg = AGM

Practical No.3

Aim: Creation of Department Table

Create table Depat ( DeptNo number(2), Dname varchar2(20), Location varcahr2(15));

1. Make DeptNo as primary key.


Alter table Depat add constraint pk2 primary key (deptno);

2. Give the command to view the details of tables Depat.


Desc Depat

3. Increase the column width by 1 for deptno in depat table.

Alter table depat modify deptno number(3);

4. Insert records in Depat table.


Insert Into Depat values(301,Web,Chennai);
Insert Into Depat values(302,HR,Bombay);
Insert Into Depat values(303,Sales,Chennai);

5. Select all records from Depat table


Select * from Depat.

Practical No.4
AIM: Study of Cursor
TYPES OF CURSORS

(1) Implicit Cursor


(2) Explicit Cursor
Implicit Cursor:
The Bank manager decided to transfer for employees across branches.Write a PL/SQL block to accept employee
number and thc branch number followed by updating the branch number of that employee to which he belongs
appropriately.Displav an appropriate message using SQL%FOUND based on the existence of the record in the
EMP_MSTR table. Displav an appropriate message using SQL%NOTFOUND based on the non-existence of the
record in the EMP_MSTR table.

BEGIN
UPDATE EMP_MSTR SET BRANCH_NO=& BRANCH_NO WHERE
EMP_NO= & EMP_NO;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT LINE(EMPLOYEE SUCCESSFULLY
TRANSFERRED):

END IF;

IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LLNE(EMPLOYEE NUMBER DOES NOT EXIST);

END IF;
END:

Example 2
FOR%ROWCOUNT
The bank manager of Darya Ganj branch decided to activate all those accounts, which were previously marked as inactive for
performing no transactions in last 365 days Write a PL/SQL block to update the status of accounts. Display an appropriate
message based on the aumber of rows affected by the update fired.
DECLARE
ROWS_AFFECTED CHAR(4);
BEGIN
UPDATE ACCT_MSTR SET STATUS=A WHERE STATUS=S AND
BRANCH_NO IN (SELECT BRANCH_NO FROM BRANCH_MSTR WHERE
NAME=DARYA GANJ);
ROWS_AFFECTED=TO_CHAR(SQL%ROWCOUNT)
IF SQL%ROWCOUNT >0 THEN
DBMS_OUTPUT.PUT_LINE(ROWS_AFFECTED ||ACCOUNT(S) ACTIVATED

SUCCESSFULLY);
ELSE
DBMS_OUTPUT.PUT_LINE (CURRENTLY THERE EXIST NO INACTIVE
ACCOUNTS IN THE DARYA GANJ BRANCH);

END IF:
END:

OUTPUT:
2 ACCOUNT(S) ACTIVATED SUCCESSFULLY
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

Explicit Cursor:
Example 3
The bank manager decided to mark all those accounts as Inactive(I) on which there are no transaction performed in
the last 365 days.whenever any such update takes place, a record for the same is maintained in the
INACTVE_ACCT_ MSTR table comprising of the account number, the opening date and the type of account. Write
a PL/SQL block to do the same.
CREATE TABLE INACTV_ACCT_MSTR(
ACCT_NO VARHAR2( 10), OPNDT DATE, TYPE VARCHAR(2));
DECLARE

CURSOR Crsr_ NoTrans IS


SELECT ACCT NO, STATUS, OPNDT, TYPE FROM ACCT MSTR
WHERE ACCT_NO IN(SELECT ACCT_NO FROM TRANS_MSTR
GROUP BY ACCT_ NO HAVING MAX(SYS DATE - DT) >365);
str_ACCT_NO ACCT_ MSTR.ACCT NO%type;

str_STATUS ACCT_MSTR.STATUS%type;
dt_OPNDT ACCT_ MSTR.OPNDT%type;
str_TYPE ACCT_MSTR.TYPE%type;
BEGIN

OPEN Crsr_NoTrans
IF Crsr_NoTrans%ISOPEN THEN
LOOP
FETCH Crsr_NoTrans INTO strACCT_NO, str_STATUS,dt_OPNDT, str_TYPE;
EXIT WHEN Crsr_NoTrans%NOTFOUND;
IF Crsr_NoTrans%FOUND THEN
UPDATE ACCT_MSTR SET STATUS=S WHERE ACCT_NO=str_ACCT_NO
INSERT INTO INACT_ACCT_MSTR
VALUES(str_ACCT_NO,dt_OPNDT,str_TYPE);
ENDIF:
END LOOP;
COMMIT:
ELSE
dbms _outputput.put_line(unable to opcn Cursor);
END IF;

CLOSE crsr_Notrans;
END;
Example 4
Write a PL/SQL block that will display the customer name,the fixed deposit number and the fixed amount of the
first 5 customers holding the highest amount in fixed deposits.
DECIARE
CURSOR Crsr_HiFD IS SELECT fNAME || ||LNAME,FD_NO,AMT
FROM CUST MSTR C,ACCT_FD_CUST_DTLS A,FD_DTLS F
WHERE C.CUST_NO=A.CUST_NO AND A.ACCT_FD_NO=F.FD_SER_NO
ORDER BY AMT DESC;
Str_NAME VARCHAR(50);
Str_FD_NO FD_DTLS.FD_NO%TYPE;

Num_AMT FD_DTLS.AMT%TYPE;
BEGIN
OPEN Crsr_hifd;
DBMS_output.put_line(NAME
FDNO AMOUNT);
DBMS_output.put_line(-------------------------------);
LOOP
FETCH Crsr_HiFD into str_NAME,str_FD_NO,num_AMT;
EXIT WHEN(Crsr_HiFD%ROWCOUNT-1) )=5 or Crsr_HiFD%NOTFOUNI);
DBMS_output.put_line (str_NAME|| ||str_FD_NO|| ||num_AMT);
END LOOP;
END;

Example5 ( using For Loop)

The bank manager has decided to mark all those accounts as inactive (I) on which there are no transactions performed in the last
365 days. Whenever any such update takes place, a record for the same is maintained in the INACTV_ACCT_MSTR table
comprising of the account number, the opening date and the type of account.
Write a PL/SQL block to do the same.
create the table INACTV_ACCT_MSTR as:
CREATE TABLE INACTV_ACCT_MSTR(
ACCTN( VARCHAR2(10), OPNDT DATE,TYPE VARCHAR2(2));
DECLARE
CURSOR Crsr_NoTrans IS
SELECT ACCT_NO, STATUS. OPNDT,TYPE FROM ACCT MSTR
WHERE ACCT_NO IN(SELEC ACCT_NO FROM TRANS_MSTR GROUP BY ACCT_NO HAVING MAX(SYSDATEDT)>365
BEGIN
FOR No Trans_Rec IN Crsr_NoTrans
LOOP
UPDATE ACCT_MSTR SET STATUS=S WHERE ACCT_NO=noTrans
Rec.ACCT NO;
INSERT INTO INACTV_ACCT_MSTR
VALUES (NoTrans_Rec.ACCT_N0,Notrans_Rec.OPNDT, NoTransRec.TYPE);

END LOOP;
COMMIT
END;
Output:
PI/SOL procedure successfull completed.
Example 6.
Wiite a Pl/SQL block of code that will merge the data available in the newly created table NEW_BRANCHES with the
data available in the table BRANCH_MSTR. if the data in the first table already exist or the second table than that data

should be skipped.

CREATE TABLE NEW BRANCHES(


BRANCHNO VARCHAR2(1O).
NAME VARCHAR2 (25));
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES(B4,MAHIM);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES(B5, Borivall);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES (B6, Darya
Ganj);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES (B7, Thane);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES (B8,

Mulund);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME)
VALUES(B9,Chernbur);
INSERT INTO NEW_BRANCHES (BRANCH_NO, NAME) VALUES(B10, Khar);

DECLARE
CURSOR Crsc_Branch_Mstr IS SELECT * FROM NEW_BRANCHES:
CURSOR Crsc_Branch _Chk (str_BRANCH_NAME varchar2) IS

SELECT BRANCH_NO FROM BRANCH_MSTR WHERE NAME = str_BRANCH_NAME;


str_BRANCH_NO BRANCH MSTR.BRANCH.NO%type;
str_BRANCH_NAME BRANCH MSTR.BRANCH.NAME%type;
mast_ins_view VARCHAR2(10);
BEGIN
OPEN Crsr_Branch_Mstr;
LOOP
FETCH Crsr_Branch_Mstr INTO str_BRANCH_NO, str_BRANCH NAME;
EXIT WHEN Crsr_Branch_Mstr%NOTFOUND;
OPEN Crsr_Branch_Chk (str_BRANCH_NAME);
FETCH Crsr_Branch_Chk INTO mast_ins_view;
IF Crsr_Branch_Chk%FOUND THEN
dbms_output.Put_Line(Branch||str_BRANCH_NAME || exist.);
ELSE
dbms_output.Put_Line(Branch||str_BRANCH_NAME || does not exist. Inserting new branch in the

database );
INSERT INTO BRANCH_MSTR VALUES (str_BRANCH_NO,
str_BRANCH_NAME);
END IF;
CLOSE Crsr_Branch_Chk;
END LOOP;
CLOSE Crsr_Branch_Mstr:
COMMIT;
END;

Practical No 5
Aim: creation of function
Consider the tables TRANS_MSTR and ACCT_MSTR. both the tables belong to the banking
system. The TRANS_MSTR table is used to register the deposits or withdrawl,as performed. As
a batch process,the initial opening balance value must be written to the TRANS_MSTR table
whenever an account is opened with the bank. Write a PL/SQL block bf code that would insert a
record representing the opening balance entry in the TRANS_MSTR table each time an account
is opened. The insert in the TRANS MSTR table depends on ACCT_NO is not present in the
TRANS MSTR table.i.e. there is no entry made for the initial deposit, then a record is inserted in

the TRANS_MSTR table representing the intial deposit ,then a record is inserted in the
TRANS_MSTR table representing the initial payment made while opening an account. If there is
already an entry.made for the initial payment ,i.e ACCT-NO is present in the TRANS MSTR table
then the insertion is skipped. To help simplify this process(of batch insertion)a function
CChkAcctNo has been created .The function checks for the existence of ACCT-NO in the table
TRANS_MSTR,the function has one argument,which receives a value. The function will search
for a matching value. in the TRANS_MSTR table when a value is passed to it when being
invoked.
The function will return 1 indicating that a match is found or 0 indicating that no match is found
this value returned by the function is used to make a decision to insert arecord or skip the
insertion in the TRANS_MSTR table.
Creation function for use
A stored function is created to perform the ACCT_NO check operation.CChkAcctNo is the name
of the function which accepts a variable ACCT_NO and returns a value to the host
environment .the value changes from (.i.e.if ACCT_NO.does not exist

) to 1(.i,e.if ACCT_NO

exists) depending on the records retrived.

Create or replace function F_CHKACCTNO(VACCT_NO IN VARCHAR2)


RETURN NUMBER IS
DUMMY ACCTNO VARCHAR2(10);
BEGIN
SELECT DISTINCT ACCT_NO INTO DUMMYACCTNO FROM TRANS_MSTR
WHERE ACCT_NO=VACCT_NO;
RETURN 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN RETURN 0;
END;

Output:
Function created.

Calling the Function F_CHKACCTNO in A PL\SQL Code Block


DECLARE
CURSOR SCANTABLE IS SELECT ACCT_NO,TYPE FROM ACCT_MSTR;
MACCT_NO ACCT_MSTR.ACCT_NO%TYPE;
MTYPE ACCT_MSTR.TYPE%TYPE;
VALEXIST NUMBER(1);
BEGIN
OPEN SCANTABLE;
LOOP
FETCH SCANTABLE INTO MACCT_NO,MTYPE;
EXIT WHEN SCANTABLE%NOTFOUND;
VALEXISTS: =F_CHKACCTNO(MACCT_NO);
IF VALEXISTS=O THEN
IF MTYPE=SB THEN
INSERT INTO TRANS_MSTR (TRANS NO,ACCT
NO,DT,TYPE,PARTICULAR, DR_CR,AMT, BALANCE)
VALUES((SELECT T ||

TO_CHAR(MAX(TO_NUMBER(SUBSTR(TRANS_NO,2)))+ 1)FROM
TRANS_MSTR),MACCT_NO,SYSDATE,C,INITIAL PAYEMENT,D,500,500);
ELSE
INSERT INTO TRANS_MSTR (TRANS NO,ACCT
NO,DT,TYPE,PARTICULAR DR_CR,AMT, BALANCE)
VALUES((SELECT T ||
TOCHAR(MAX(TO_NUMBER(SUBSTR(TRANS_NO,2)))+ 1)FROM
TRANS_MSTR),MACCT_NO,SYSDATE/C,INITIAL PAYEMENT,D,200,200);
END IF;
END IF;
END LOOP;
CLOSE SCANTABLE;
COMMIT;
END;

Output
PL/SQL procedure successfully completed.
Deleting Function:

DROP FUNCTION F_CHLACCTNO;

Practical No 6
Aim: Creation of stored Procedure
Using a Procedure

Example:
The current example deals with the Fixed Deposits Maturity system. The tables involved with
this processing are FD_DTLS, Trans_MSTR and ACCT_MSTR. Whenever a fixed deposit is due
for payment and if there are instructions given to the bank for crediting the fixed deposit amount
on maturity to their account held in the bank, an entity is passed in the TRANS_MSTR table for
deposit of the fixed deposit amount as well as the acct_mstr table is updated to reflect the
increase of the current balance. Finally the status of that fixed deposits is updated to reflect the
increase of the current balance. Finally the status of that fixed deposit is updated as Mie.
Matured in the FD_DTLS table.
In order to simplify the job of insertion and updating of three tables( Trans_MSTR, FD_DTLS
and ACCT_MSTR) each time a fixed deposit is due for payment a procedure is created. This
procedure is then called in the PL/SQL block while checking for those fixed deposits due for
payment.

Creating Procedure for use


A procedure called PROC_INSUPD is created and stored in the database. This procedure when
called in a PL/ SQL block updates the current balance in the ACCT_MSTR table and the status
in the FD_DTLS table. It also inserts an entry to register the credit of the fixed deposit amount in
the TRANSMSTR table.
TABLE CREATION FOR FD_DTLS
CREATE TABLE FD_DTLS(FD_NO INT,PAYTO_ACCTNO INT,DUEAMT INT);
INSERT INTO FD_DTLS VALUES(1,1,1500);
INSERT INTO FD_DTLS VALUES(2,2,2000);
INSERT INTO FD_DTLS VALUES(3,3,2400);

INSERT INTO FD_DTLS VALUES(4,4,5300);


CREATING PROCEDURE:
CREATE OR REPLACE PROCEDURE PROC_INSUPD(VFD NO IN
VARCHAR2,VACCTNO IN VARCHAR2,VAMT IN NUMBER)
IS
MCURBAL NUMBER;
BEGIN
SELECT CURBAL INTO MCURBAL FROM ACCT MSTR WHERE ACCT_NO=VACCT_NO;
INSERT INTO
TRANS_MSTR(TRANS_NO,ACCT_NO, DT,TYPE, PARTICULAR,DR_CR,AMT,BALANCE)
VALUES
((SELECT T||TO_CHAR(MAX(TO_NUMBER(SUBSTR(TRANS_NO,2)))+ 1)
FROM TRAN_SMSTR),VACCT_NO,SYSDATE,C,FIXED DEPOSIT
PAYEMENT,D,VAMT,(MCURBAL+VAMT));
UPDATE ACCT_MSTR SET CURBAL=CURBAL+VAMT WHERE ACCT_NO=VACCT_NO;
UPDATE FD_DTLS SET STATUS=M WHERE FD_NO=VFD_NO;
END;
DECLARE
CURSOR CRSR_FDCHK IS
SELECT FD_NO, PAYTO_ACCTNO,DUEAMT FROM FDDTLS
WHERE TO_CHAR(DUEDT,DD-MM-YY)=TO_CHAR(SYSDATE,DD-MM-YY)
AND STATUS=A AND PAYTO_ACCTNO IS NOT NULL;
MFD_NO VARCHAR2(1O);
MPAYTO_ACCTNO VARCHAR2(1O);
MAMT NUMBER(8,2);
MSTATE NUMBER:O;
BEGIN
OPEN CRSR_FDCHK;
LOOP
FETCH CRSR_FDCHK INTO MFD_NO,MPAYTO_ACCTNO,MAMT;
EXIT WHEN CRSR_FDCHK%NOTFOUND;
PRO_INSUPD(MFD_NO,MPAYTO_ACCTNO, MAMT);
M STATE: =1;
END LOOP;
IF MSTATE=O THEN
DBMS_OUTPUT.PUTLINE(CURRENTLY THERE ARE NO FIXED DEPOSIT DUE FOR
PAYEMENT);
END IF;

CLOSE CRSR_FDCHK;
COMMIT;
END;

OUTPUT:

Currently there are no fixed deposit due to payement.


PL/SQL procedure successfully completed.

Deleting a stored procedure

DROP PROCEDURE PROC_INSUPD;

Practical No. 7
Aim: Creating Package
The first step to creating a package is to create its specification. The specification declare the objects that are
contained in the body of the package. A package is created using Oracles SQL * Plus inyeractive tool. A package
can include Functions and Procedures. The variable declared within the package can be accessed by any Function or
Procedure within the Package. To create a specification, issue the CREATE PACKAGE command:
Example 1:
CREATE OR REPLACE PACKAGE TRANSACTION_MGMT AS PROCEDURE
PERFORM TRANS(mACCT NO VARCHAR2, NUMBER MD VARCHAR2, AMT NUMBER);

PROCEDURE CANCEL_FD(mFD_NO VARCHAR2);


BAL NUMBER;
P_ACCT_NO VARCHAR2(1O);
D_AMT NUMBER;
END TRANSACTION_MGMT;

Output:
Package created.
CREATE OR REPLACE PACKAGE BODY TRANSACTION_MGMT AS PROCEDURE
PERFORM_TRANS(mACCT_NO VARCHAR2, MD VARCHAR, AMT NUMBER) IS
BEGIN
SELECT CURBAL INTO BAL FROM ACCT MSTR WHERE ACCT_NO= mACCT_NO;
IF MD=DTHEN
INSERT INTO TRANS_MSTR(TRANS_NO, ACCT NO, DT,TYPE, PARTICULAR, DR_CR,
AMT, BALANCE) VALUES(SELECT T II TO_CHAR(TO_NUMBER(SUBSTR(TRANS_NO
,2)))+1) FROM TRANS_MSTR), mACCT_NO, SYSDATE, C, DEPOSIT, MD,AMT,(BAL
+ AMT));
UPDATE ACCT_MSTR SET CURBAL=CURBAL+AMT
WHERE ACCT_NO=mACCTNO;
ELSE
IF AMT<BAL THEN
INSERT INTO TRANS_MSTR(TRANS NO, ACCT_NO, DT,TYPE,APRTICULAR, DR_CR,
AMT, BALANCE)
VALUES((SELECT T || TO_CHAR(MAX(TO_NUMBER(SUBSTR(TRANS_NO,2))) + 1)
FROM TRANS_MSTR), mACCT_NO, SYSDATE, C, Withdrawal, MD, AMT,(BAL_AMT));
UPDATE ACCT_MSTR SET CURBAL=CURBAL -AMT WHERE ACCT_NO= mACCT_NO;
END IF;
END IF;
END;
PROCEDURE CANCEL_FD(mFD_NQ VARCHAR2) IS
BEGIN
SELECT PAYTO_ACCTNO,(AMT * (SYSDATE-OPNDT)/365* INTRATE)/100 + AMT
INTO P_ACCT_N0, D_AMT FROM FD_DTLS WHERE FD_NO= mFD_NO;
SELECT CURBALINTO BAL FROM ACCT_MSTR WHERE ACCT_NO (SELECT
PAYTO_ACCTNO FROM FD_DTLS WHERE FD_NO= mED_NO);
IF P_ACCT_NO IS NOT NULL THEN
INSERT INTO TRANS_MSTR(TRANS_NO, ACCT_NO, DT, TYPE, PARTICULAR,
DR_CR, AMT, BALANCE) VALUES(SELECT T ||

TO_CHAR(MAX(TO_NUMBER(SUBSTR(TRANS_NO, 2)))+ 1) FROM


TRANS_MSTR),P_ACCT_NO, SYSDATE, C, FIXED DEPOSIT PAYMENT, D,
D_AMT,(BAL+ D_AMT);

UPDATE ACCT_MSTR SET CURBAL=CURBAL+ D_AMT WHERE ACCT_NO


PACCT_NO;
DBMS_OUTPUT.PUTLINE( Your FD is cancelled, the amount is credited to the
account number:|| P_ACCT_NO);
ELSE
DBMS_OUTPUT.PUTLINE( YOUR FD IS CANCELLED. COLLECT THE AMOUNT FROM
THE CASH COUNTER.);
END IF;

UPDATEFD_DTLSSETSTATUS=CWHEREFDNO=mFD_NO;
END;
END TRANSACTION_MGMT;

OUTPUT:
Package body created.

INVOKING PACKAGE MEMBERS

To reference the variable BAL in the package named TRANSACTION_MGMT, the


referencing statement would be:
DECLARE Balance number;

BEGIN
IFTRANSACTION_MGMT.BAL<balanceTHEN
END IF;
The first procedure defined in the above package can be executed as follows:
EXECUTE TRANSACTION_MGMT. PERFORM_TRANS(SBI, D,SOOO);
OUTPUT:
PL/SQI procedure successfully completed.
or
CALL TRANSACTION_MGMT.PERFORM_TRANS(SBI,D,SOOO);

Example 2:
Create a package comprising of a procedure and a function.
the function will:
Accept the branch number and calculate the number of employees in that branch and finally return the
number of employees
The procedure will:

accept the branch number


Using the function created will get the employee count for the branch number accepted
Based on the employee count a decision will be taken to delete the employees belonging to that
branch followed by deleting the branch.

Package Specification
CREATE OR REPLACE PACKAGE PCK_DEL IS PROCEDURE
DEL_EMP_BRANCH(mBRANCH_NO VARCHAR2);
FUNCTION CNT EMP BRANCH(mBRANCH_NO VARCHAR2) RETURN NUMBER;
END PCK_DEL;

Output:
Package created.

Package Body
CREATE OR REPLACE PACKAGE BODY PCK_DEL IS PROCEDURE
DELEMP_BRANCH(mBRANCH_NO VARCHAR) IS noemp number;
BEGIN
noemp: =CNT_EMP_BRANCH(mBRANCH_NO);
IF noemp<2 AND noemp> 0 THEN
DELETE EMP MSTR WHERE BRANCH_NO=mBRANCH_NO;
DBMS_OUTPUT.PUT_LINE(AII the employees belonging to the branch
mBranchNo I deleted successfully);

DELETE Branch_MSTR WHERE BRANCH_NO= mBRANCHNO;


DBMS_OUTPUT.PUT_LINE(Branch || mBRANCH_NO || deleted successfully);
END IF;
IF noemp=0 then
DBMS OUTPUT.PUT LINE(there exist no employee in the branch);
end if;
IF noemp>=2 then

DBMS_OUTPUT.PUT_LINE(There exist || noempi || employee in the branch


mBRANCH_NO || Skipping Deletetion);

END IF;
END;
FUNCTION CNT EMP BRANCH)mBRANCH_NO VARCHAR) RETURN NUMBER IS
noemp NUMBER;
BEGIN
SELECT COUNT (*) INTO noemp FROM EMP_MSTR WHERE
BRANCH NO= mBRANCHNO;
RETURN noemp;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN 0;

END;
END PCK_DEL;
Output:
Package Body created.
OVERLOADING PROCEDURES AND FUNCTIONS
EXAM PIE 1:
Create a package to check that a numeric value is greater than zero and a date is
less than or equal to sysdate.
CREATE OR REPLACE PACKAGE CHECK_FUNC IS FUNCTION VALUE_OK(DATE_IN IN
DATE) RETURN VARCHAR2;
FUNCTION VALUE_OK(NUMBER_IN IN NUMBER) RETURN VARCHAR2;
END;
OUTPUT:
PACKAGE CREATED.
CREATE OR REPLACE PACKAGE BODY CHECK FUNC IS FUNCTION VALUE_OK
(DATE_IN IN DATE) RETURN VARCAHR2 IS BEGIN
IF DATE_IN <= SYSDATE THEN
RETURN Output From the First-Over loaded Function: true;
ELSE
RETURN OUTPUT FROM THE FIRST OVER LOADED FUNCTION: FALSE;
END IF;
END;
FUNCTION VALUE_OK(NUMBER_IN IN NUMBER) RETURN VARCHAR2 IS
BEGIN
IF NUMBER_IN>O THEN
RETURN OUTPUT FROM THE SECOND OVER LOADED FUNCTION:TRUE;
ELSE
RETURN OUTPUT FROM THE SECOND OVER LOADED FUNCTION: FALSE;
END IF;
END;
END;
OUTPUT:
package body created.
Example 2:

The bank manager decides to activate all those accounts, which were previously marked as
inactive or performing no transactions in last 365 days.
Create a package spec and a package body named ACCT_MNTC that includes two procedures
of the same name and the second procedure accepts branch name.
Package Specification:
CREATE OR REPLACE PACKAGE ACCT_MNTC IS PROCEDURE
ACT_ACCTS(vBRANCH_NO IN NUMBER);
PROCEDURE ACT ACCTS(vNAME IN VARCHAR2);
END;

OUTPUT:
Packagecreated.
Package Body:
CREATE OR REPLACE PACKAGE BODY ACCT_MNTC IS PROCEDURE
ACT_ACCTS(vBRANCH_NO IN NUMBER) IS
BEGIN
UPDATEACCT_MSTRSETSTATUS=A
WHEREBRANCHNO=B|| vBRANCH_NOANDSTATUS=S
IFSQL%ROWCQNT>0THEN
DBMSOUTPUT.PUT_LINE(TO_CHAR(sQL%ROWCOUNT)|| ACCOUNTS ACTIVATED
SUCCESSFULLY);
ELSE
DBMS_OUTPUT. PUT_LINE(CURRENTLY THERE EXIST NO INACTIVE ACCOUNTS IN
THE BRANCH NO || vBRANCH_NO);
END IF;
END;
PROCEDUREACT_ACCTS(vNAMEINVARCHAR2)IS
BEGIN
UPDATEACCT_MSTRSETSTATUS=AWHERESTATUS=S
ANDBRANCH_NOIN(SELECTBRANCH_NOFROMBRANCH_MSTRWHERENAME=
vNAME);
IF SQL%ROWCOUNT>0 THEN
DBMS_OUTPUT. PUT_LINE(TO CHAR(SQL%ROWC0UNT) || ACCOUNTS ACTIVATED
SUCCESSFULLY);
ELSE
DBMS_OUTPUT.PUT_LINE(Currently there exist no Inactive Accounts in the branch vName);

ENDIF;
END;
ENDACCT_MNTC;

Output:
Package body created.
EXECUTE ACCTMNTC.ACTACCTS(1);
EXECUTE ACCT_MNTC.ACCT ACCTS(Vile Parle);

Practical No 8
Aim: Creating Triggers
Example 1
Create a transparent audit system for a table CUST_MSTR. The system must keep track of the

records that are being deleted or updated. The functionality being when a record is deleted or
modified the original record details and the date of operation are stored in the audit table, then
the delete or update operation is allowed to go through.
The table definition for audit system is given below.
CREATE TABLE AUDIT CUST(
CUST_NO VARCHAR2(20),FNAME VARCHAR2(25),MNAME VARCHAR2(25),
LNAME VARCHAR2(25),DOB_INC DATE NOT NULL,OCCUP VARCHAR2(25),
PHOTOGRAPH VARCHAR2(25),PANCOPY VARCHAR2(1),FQRM6O VARCHAR2(1),
OPERATION VARCHAR2(20),USERID VARCHAR2(20),OPDATE DATE);

Output:

Table created.
CREATE TRIGGER AUDIT TRAIL
AFTER UPDATE OR DELETE ON CUST_MSTR
FOR EACH ROW
DECLARE
OPEN VARCHAR2(8);
BEGIN
IF UPDATING THENOPEN:=UPDATE;
END IF;
IF DELETING THENOPEN:=DELETE;
INSERT INTO AUDIT_CUST VALUES(:OLD.CUST NO,:OLD.FNAME, :OLD.MNAME,
:OLD, LNAME, : OLD.DOB IN,: OLDCOCCUP
:OLD. PHOTOGRAPH , : OLD.PANCOPY, OLD.FORM6O,OPER,USER,SYSDATE);
END;
Output:
Trigger created.

Example 2
Write a data trigger on the TRANS MSTR that checks the following:
The account number for which the transaction is being performed is.a valid account number.
The Transaction Amount is not zero and is positive and In case of a withdrawal the amount does
not exceed the current balance for that account number.

Create or replace trigger TRANS_CHECK


BEFORE INSERT ON TRANS MSTR ON EACH ROW
DECLARE
V_CNT_ACCT_NO VARCHAR2(20);
VCURBAL NUMBER(1O);
BEGIN
SELECT COUNT(ACCT_NO)INTO V_C NT_ACCT_NO
FROM ACCT_MSTR WHERE ACCT_NO= NEW,ACCT_NO;
IF VCNT_ACCT_N0=0 THEN
RAISE_APPLICATION_ERROR(-20000.THE ACCOUNT NO IS INVALID);
END IF;
IF NEW.AMT<=0 THEN
RAISE_APPLICATION_ERROR(-2000 1 .TH E TRANSACTION AMOUNT CANT BE
NEGATIVE);
END IF;
SELECT CURBAL INTO V_CURBAL;
FROM ACCT_MSTR WHERE ACTT_NO=:NEW.ACCT_NO;
IF V_CURBAL< :NEW.AMT AND :NEW .DR_CR= W THEN
RAISE_APPLICATION_ERROR(-20002.THE AMOUNT OF WITHDRAWAL CANNOT
EXCEED THE BALANCE HELD IN THE ACCOUNT );
END IF;
END;
Output
Trigger created.

Example 3
Generating Primary Key Using Sequences
Sequence Creation
CREATE SEQUENCE CUST_SEQ INCREMENT BY 1 START WITH 1;

Output:
Sequence created.

Database Trigger Creation

CREATE OR REPLACE TRIGGER CUST_NO_GENERATION


BEFORE INSERT ON CUST_MSTR FOR EACH ROW
DECLARE

PRIMARY_KEY_VALUE VARCHAR2(1O);
BEGIN
SELECT C || TO_CHAR(CUST_SEQ.NEXTVAL)INTO
PRIMARY_KEY_VALUE FROM DUAL;
:NEW.CUST_NO: = PRIMARY_KEY_VALUE;
END;

Output:

Trigger created.

Practical No. 9
Aim: Write a program that aceepts name and display it 5 times in output screen.

DECLARE
NAME VARCHAR2(15) := &NAME;
N NUMBER(2) := 1;
BEGIN
WHILE N <= 5
LOOP
DBMS_OUTPUT.PUT_LINE(NAME);
N := N + 1;
END LOOP;
END

Output :
SQL
SQL
SQL
SQL
SQL

Practical No. 10
Aim: Write a program that aceepts principal, duration (in months), and rate of interest & calculate
Maturity Amount

DECLARE
PRINCIPAL NUMBER := &PRINCIPAL;
MONTHS NUMBER := &MONTHS;
ROI NUMBER := &ROI
MAMOUNT NUMBER;
BEGIN
MAMOUNT := PRINCIPAL + (PRINCIPAL * ROI/100) * MONTHS;
DBMS_OUTPUT.PUT_LINE(MAMOUNT);
END

Output :
PRINCIPAL = 4000
ROI = 5%
MONTHS = 6
MAMOUNT = 5200

Practical No. 11
Aim: Write a program that aceepts radius & calculate Area of circle. Use PI as Constant.

DECLARE
RADIUS NUMBER := & RADIUS;
PI NUMBER := 3.14;
AREA NUMBER;
BEGIN
AREA:= PI * (RADIUS * RADIUS);
DBMS_OUTPUT.PUT_LINE(AREA IS ||AREA);
END

Output :
RADIUS = 40
AREA IS 5024.

Practical No. 12
Aim: Write a program that aceepts number & display whether it is less than, or equal to, or greater
than 10.

DECLARE
N NUMBER := 5;
BEGIN
IF N = 10 THEN
DBMS_OUTPUT.PUT_LINE(GIVEN NUMBER IS EQUAL TO 10);
ELSE IF N < 10 THEN
DBMS_OUTPUT.PUT_LINE(GIVEN NUMBER IS LESS THAN 10);
ELSE IF N > 10 THEN
DBMS_OUTPUT.PUT_LINE(GIVEN NUMBER IS GREATER THAN 10);
END IF;
END

Output :
GIVEN NUMBER IS LESS THAN 10.

You might also like