DBMS Practicals Sem 3 Mca Idol | Shree Ram College

  • Upload
    unknown

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    1/34

     

    UNIVERSITY OF MUMBAI

    INSTITUTE OF DISTANCE AND OPEN LEARNING (IDOL)*

    *(Institute of Distn!e E"u!tion (IDE) is #en$e")

    CERTIFICATE

     T%E E&PERIMENTS DULY SIGNED IN T%IS 'OURNAL REPRESENT

     T%E BONAFIDE OR BY MST B%US%AN PRADEEP OLAMBEAR

    IN SEMESTER III  OF SECOND YEAR OF MASTER IN

    COMPUTER APPLICATION (MCA)  IN T%E COMPUTER

    LABORATORY OF PCP CENTER SHREE RAM COLLAGE,BHANDUP  FOR SUB'ECT DATABASE MANAGEMENT SYSTEM

    DURING ACADEMIC YEAR +,-.+,/0

     11111111111111111111 

     1111111111111111111111111111 LECTURE IN C%ARGE %EAD2 DEPARTEMENT OF

    MCA

      1111111111111111111111 

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    2/34

      E&TERNAL E&AMINER

    INDEX

    SR. NO. TITLE SIGN

    1CREATION OF BANKING ENTERPRISE DATABASE.

    2CREATON OF EMPLOYEE TABLE

    3CREATION OF DEPARTMENT TABLE

    4TYPES OF CURSOR.

    5CREATION OF FUNCTION.

    6CREATION OF STORED PROCEDURE.

    7CREATION OF PACKAGE.

    8CREATION OF TRIGER.

    9 RITE A BLOCK T!AT ACCEPTS NAME AND DISPLAY IT 5

    TIMES IN OUTPUT SCREEN.

    1" RITE A PL#S$L BLOCK T!AT ACCEPTS

    PRINCIPAL%DURATION&IN MONT!S' AND RATE OF INTEREST

    AND CALCULATE MATURITY AMOUNT.

    11 RITE A BLOCK T!AT ACCEPTS RADIUS AND CALCULATE

    AREA OF CIRCLE. USE PI AS CONSTANT.

    12 RITE A BLOCK T!AT ACCEPTS NUMBER AND DISPLAY!ET!ER IT IS LESS T!AN% OR E$UAL% OR GREATER T!AN

    1".

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    3/34

    Practical No.1

    A()* C+,-(/0 / -0(0 E0,++(, D---,

    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’)

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    4/34

    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)

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    5/34

    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

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    6/34

    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 allcustomerswhose street addressincludesthe 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.

     

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    7/34

     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’)

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    8/34

    20.Find the branch that has highest average balance

    Select br_name

    From branch

    Group by br_nameHaving 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’)

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    9/34

    Practical No.2

    A()* C+,-(/0 / E)/,, T-,

    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.

    A3te# t43e E$53o6ee "" !onst#int 57 5#i$#6 7e6 (e$5no)8

    2.Select all employees who are under the manager disgnation or have salary grater than

    10000.

    Se3e!t E$5No2 En$e2 "esi92 s3#6 f#o$ E$53o6ee :;e#e "esi9n <

    =Mn9e#> o# s3e6 ? ,++++0

    3.List all employees whose name begin with@v

    Se3e!t * f#o$ E$53o6ee :;e#e En$e 3i7e =>

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    10/34

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

    Se3e!t * f#o$ E$53o6ee O#"e# 46 S3#6 Des!

    5.Find the highest & lowest salaries & the difference between them.

    Se3e!t M(s3#6)2 Min(s3#6)2 M(s3#6).Min(s3#6) f#o$ E$53o6ee0

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

    U5"te E$53o6ee set S3#6 < ,0, * s3#6 :;e#e "esi9 < =Mn9e#>

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

    Se3e!t su$(s3#6)2 su$(!o$$) f#o$ E$53o6ee :;e#e "esi9 < =Mn9e#>

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

    Se3e!t * f#o$ E$53o6ee :;e#e "o < >,+.$6.++>

    9.Fin" t;e e#9e s3#6 of AGM0

    Se3e!t 9(s3#6) f#o$ e$53o6ee :;e#e "es9 < =AGM>

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    11/34

    Practical No.3

    A()* C+,-(/0 / D,-+),0 T-,

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

    1.Make DeptNo as primary key.

    A3te# t43e Depat"" !onst#int 57 5#i$#6 7e6 ("e5tno)8

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

    Des! De5t

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

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    12/34

    A3te# t43e "e5t $o"if6 "e5tno nu$4e#()8

    4.Insert records in Depat table.

    Inse#t Into De5t 3ues(+,2>e4>2>C;enni>)8

    Inse#t Into De5t 3ues(+2>%R>2>Bo$46>)8

    Inse#t Into De5t 3ues(+2>S3es>2>C;enni>)8

    5.Select all records from Depat table

    Se3e!t * f#o$ De5t0

    Practical No.4

    AIM* S: / C+/+

    TYPES OF CURSORS

    (1) Implicit Cursor

    (2) Explicit Cursor

    Implicit Cursor:

    Th !"#$ m"#"%r &ci&& to tr"#s'r 'or mplos "cross r"#chs*+rit " P,-S., loc$ to "ccpt mplo

    #umr "#& thc r"#ch #umr 'ollo/& up&"ti#% the r"#ch #umr o' th"t mplo to /hich h lo#%s

    "ppropri"tl*0ispl" "# "ppropri"t mss"% usi#% S.,FOU30 "s& o# th xist#c o' th rcor& i# thE4P54STR t"l* 0ispl" "# "ppropri"t mss"% usi#% S.,3OTFOU30 "s& o# th #o#6xist#c o' th

    rcor& i# th E4P54STR t"l*

    BEGIN

    UPDATE EMP;MSTR SET BRANC!;NOFOUND T!EN

    DBMS;OUTPUT.PUT LINE&?EMPLOYEE SUCCESSFULLYTRANSFERRED@'*

    END IF

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    13/34

    IF S$L>NOTFOUND T!EN

    DBMS;OUTPUT.PUT;LLNE&@EMPLOYEE NUMBER DOES NOT EXIST@'

    END IFEND*

    Ex"mpl 2 

    FORRO+COU3TTh "#$ m"#"%r o' 0"r" 7"#8 r"#ch &ci&& to "cti"t "ll thos "ccou#ts9 /hich /r priousl m"r$& "s i#"cti 'or

     pr'ormi#% #o tr"#s"ctio#s i# l"st ;< &"s +rit " P,-S., loc$ to up&"t th st"tus o' "ccou#ts* 0ispl" "# appropriate 

    mss"% "s& o# th "umr o' ro/s "''ct&   th up&"t 'ir&*

    DECLARE

    ROS;AFFECTED C!AR&4'

    BEGIN

    UPDATE ACCT;MSTR SET STATUSROCOUNT >0 THEN

    DBMS;OUTPUT.PUT;LINE&ROS;AFFECTED  ACCOUNT&S' ACTIATED

    SUCCESSFULLY@'

    ELSE

    DBMS;OUTPUT.PUT;LINE &@CURRENTLY T!ERE EXIST NO INACTIE

    ACCOUNTS IN T!E DARYA GAN BRANC!@'

    ENDIF*END*

    OUTPUT* 

    2 ACCOUNT&S' ACTIATED SUCCESSFULLYP,-S., PROCE0URE SUCCESSFULLY COMPLETED.

    Explicit Cursor:

    Ex"mpl

    Th "#$ m"#"%r &ci&& to m"r$ "ll thos "ccou#ts "s I#"cti(I) o# /hich thr "r #o tr"#s"ctio# pr'orm& i#

    th l"st ;< &"s*/h#r "# such up&"t t"$s pl"c9 " rcor& 'or  th s"m is m"i#t"i#& i# thI3=CT>E5=CCT5 4STR t"l comprisi#% o' th "ccou#t #umr9 th op#i#% &"t "#& th tp o' "ccou#t* +rit

    " P,-S., loc$ to &o th s"m*

    CRE=TE T=!,E I3=CT>5=CCT54STR(=CCT53O >=R?=R2( 1"'% OP30T 0=TE9 TYPE >=RC?=R(2))@ DECLARE

    CURSOR C++; N/T+-0 IS

    SELECT ACCT NO% STATUS% OPNDT% TYPE FROM ACCT MSTR

    !ERE ACCT;NO IN&SELECT ACCT;NO FROM TRANS;MSTR

    GROUP BY ACCT; NO !AING MAX&SYS DATE DT' 365'

    +;ACCT;NO ACCT; MSTR.ACCT —NO>,

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    14/34

    +; STATUS ACCT;MSTR.STATUS>,:;OPNDT ACCT; MSTR.OPNDT>,

    +;TYPE ACCT;MSTR.TYPE>,BEGIN

    OPE3 Crsr53oTr"#s

    IF Crsr53oTr"#sISOPE3 T?E3

    ,OOP

    FETC? Crsr53oTr"#sINTO strACCT_NO,str5ST=TUS9&t5OP30T9 str5TYPE@

    EAIT +?E3 Crsr53oTr"#s3OTFOU30@IF Crsr53oTr"#sFOU30 T?E3

    UP0=TE =CCT54STR SET ST=TUSB’S’ +?ERE =CCT53OBstr5=CCT53O

    INSERT INTOI3=CT5=CCT54STR >=,UES(str5=CCT53O9&t5OP30T9str5TYPE)@

    E30IF:

    E30 ,OOP@

    CO44IT:E,SE

    dbms _outputput.put_line(?unable toopcn Cursor’);END IF;

    C,OSE crsr53otr"#s@E30@

    Ex"mpl

    +rit " P,-S., loc$ th"t /ill &ispl" th customr #"m9th 'ix& &posit #umr "#& th 'ix& "mou#t o' th

    'irst < customrs hol&i#% th hi%hst "mou#t i# 'ix& &posits*

    0ECI=RE

    CURSOR Crsr5?iF0 IS SE,ECT '3=4E DD’ ‘DD,3=4E9F053O9=4T

    FRO4 CUST 4STR C9=CCT5F05CUST50T,S =9F050T,S F+?ERE C*CUST53OB=*CUST53O =30 =*=CCT5F053OBF*F05SER53O

    OR0ER !Y =4T 0ESC@Str53=4E >=RC?=R(

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    15/34

    Th "#$ m"#"%r h"s &ci&& to m"r$ "ll thos "ccou#ts "s i#"cti (I) o# /hich thr "r #o tr"#s"ctio#s pr'orm& i# th l"st;< &"s* +h#r "# such up&"t t"$s pl"c9 " rcor& 'or th s"m is m"i#t"i#& i# th I3=CT>5=CCT54STR t"l

    comprisi#% o' th "ccou#t #umr9 th op#i#% &"t "#& th tp o' "ccou#t*

    +rit " P,-S., loc$ to &o th s"m*

    cr"t th t"l I3=CT>5=CCT54STR "s:

    CRE=TE T=!,E I3=CT>5=CCT54STR(=CCT3( >=RC?=R2(1)9 OP30T 0=TE%TYPE >=RC?=R2(2))@ 

    0EC,=RE

    CURSOR Crsr53oTr"#s IS 

    SE,ECT =CCT53O9 ST=TUS* OP30T9TYPE FRO4 =CCT 4STR+?ERE =CCT53O I3(SE,EC =CCT53O FRO4 TR=3S54STR 7ROUP !Y =CCT53O ?=>I37 4=A(SYS0=TE6

    0T);<

    !E7I3

    FOR 3o Tr"#s5Rc I3 Crsr53oTr"#s,OOP

    UP0=TE =CCT54STR SET ST=TUSB’S’ +?ERE =CCT53OB#oTr"#s

    Rc*=CCT NO

    INSERT INTO INACTV_ACCT_MSTR 

    >=,UES (3oTr"#s5Rc*=CCT5  393otr"#s5Rc*OP30T9 3oTr"#sRc*TYPE)@E30 ,OOP@CO44IT

    E30@

    Out put:PI#SOL proc&ur succss'ull complt&*

    Ex"mpl ;*

    +iit " Pl-S., loc$ o' co& th"t /ill ),+, th :-- --(-, (0 H, #/l +,-,: -, NE;BR=3C?ES /ith H,

    :-- --(-, (0 H, -, !R=3C?5 MSTR. ( H, &"t" i# th 'irst t"l "lr"& xist or th sco#& -, H-0 th"t :--

    H/: , (,:.

     

    CRE=TE T=!,E 3E+ !R=3C?ES(

    !R=3C?3O >=RC?=R2(1O)*

     3=4E >=RC?=R2 (2=,UES(‘!’9’4=?I4’)@

    I3SERT I3TO 3E+5!R=3C?ES (!R=3C?53O9NAME)>=,UES(’!=,UES (’!G’9 ‘Th"#’)@

    I3SERT I3TO 3E+5!R=3C?ES (!R=3C?53O9NAME)>=,UES (‘!H’9

    ‘Mulund’);

    I3SERT I3TO 3E+5!R=3C?ES (!R=3C?53O9 3=4E)>=,UES(’!’9’Chr#ur’)@

    I3SERT I3TO 3E+5!R=3C?ES (!R=3C?53O9NAME)>=,UES(‘!1’9 ‘Jh"r’)@

    0EC,=RE

    CURSOR Crsc5!r"#ch54str IS SE,ECT *FRO4 3E+5!R=3C?ES:

    CURSOR Crsc5!r"#ch 5Ch$ (str5!R=3C?53=4E "rch"r2) IS

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    16/34

    SE,ECT !R=3C?53O FRO4 !R=3C?54STR +?ERE 3=4E =str5!R=3C?53=4E@

    str5!R=3C?53O !R=3C? 4STR*!R=3C?*3Otp@

    str5!R=3C?53=4E !R=3C? 4STR*!R=3C?*3=4Etp@

    mast_ins_view>=RC?=R2(1)@!E7I3

    OPE3 Crsr5!r"#ch54str@

    ,OOP

    FETC? Crsr5!r"#ch54str I3TO str5!R=3C?53O9 str5!R=3C? 3=4E@

    EAIT +?E3 Crsr5!r"#ch54str3OTFOU30@

    OPE3 Crsr5!r"#ch5Ch$ (str5!R=3C?53=4E)@

    FETC? Crsr5!r"#ch5Ch$ I3TO m"st5i#s5i/@

    IF Crsr5!r"#ch5Ch$FOU30 T?E3

    &ms5output*Put5,i#(’!r"#ch’DDstr5!R=3C?53=4E DD ‘xist*’)@E,SE

    &ms5output*Put5,i#(’!r"#ch’DDstr5!R=3C?53=4E DD’does#ot xist* I#srti#%new r"#chinth

    database’ );

    I3SERT I3TO !R=3C?54STR >=,UES (str5!R=3C?53O9

    str5!R=3C?53=4E)@

    E30 IF@

    C,OSE Crsr5!r"#ch5Ch$@

    E30 ,OOP@

    C,OSE Crsr5!r"#ch54str:

    CO44IT@

    E30@

    P+-(- N/ 5

    A()* +,-(/0 / 0(/0

     

    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

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    17/34

    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 a’record 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 nameof 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_NOexists) 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.

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    18/34

    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 THENIF 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:

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    19/34

    DROP FUNCTION F_CHLACCTNO;

    Practical No 6

    A()* C+,-(/0 / /+,: P+/,:+, 

    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);

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    20/34

    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 DEPOSITPAYEMENT’,’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;

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    21/34

    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;

    P+-(- N/. 7

    A()* C+,-(0 P--,

    Th 'irst stp to cr"ti#% " p"c$"% is to cr"t its spci'ic"tio#* Th spci'ic"tio# &cl"r th o8cts th"t "r

    co#t"i#& i# th o& o' th p"c$"%* = p"c$"% is cr"t& usi#% Or"cl’s S., K Plus i#r"cti tool* = p"c$"%c"# i#clu& Fu#ctio#s "#& Proc&urs* Th "ri"l &cl"r& /ithi# th p"c$"% c"# "ccss& "# Fu#ctio# or

    Proc&ur /ithi# th P"c$"%* To cr"t " spci'ic"tio#9 issu th CRE=TE P=CJ=7E comm"#&:

     

    Example 1:

    CREATE OR REPLACE PACKAGE TRANSACTION_MGMT AS PROCEDURE

    PERFORM TRANS(mACCT NO VARCHAR2, NUMBER MD VARCHAR2, AMT NUMBER);

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    22/34

    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=’D’THEN

    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

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    23/34

    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 FROMTHE CASH COUNTER.’);

    END IF;

    UPDATE FD_DTLS SET STATUS=’C’ WHERE FDNO=mFD_NO;

    END;

    END TRANSACTION_MGMT;

    OUTPUT:

    Package body created.

    INOKINGPACKAGE MEMBERS

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

    referencing statement would be:

    DECLARE Balance number;

    BEGIN

    IF TRANSACTION_MGMT.BAL< balance THEN

    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.

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    24/34

    Package Specification

    CREATE OR REPLACE PACKAGE PCK_DEL IS PROCEDURE

    DEL_EMP_BRANCH(mBRANCH_NO VARCHAR2);

    FU3CTIO3CNT EMP BRANCH(mBRANCH_NO VARCHAR2) RETURN NUMBER;E30PCK_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 0 THEN

    DELETE EMP MSTR WHERE BRANCH_NO=mBRANCH_NO;

    DBMS_OUTPUT.PUT_LINE(‘AII the employees belonging to the branch’

    mBranchNoI‘deleted successfully’);DELETE Branch_MSTR WHERE BRANCH_NO= mBRANCHNO;

    DBMS_OUTPUT.PUT_LINE(‘Branch’||mBRANCH_NO||‘deleted successfully’);END IF;

    IF noemp=0 thenDBMS 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;

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    25/34

    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_INO 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.

    Example2:

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    26/34

    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:

    Package created.

    Package Body:

    CREATE OR REPLACE PACKAGE BODY ACCT_MNTC IS PROCEDURE

    ACT_ACCTS(vBRANCH_NO IN NUMBER) IS

    BEGINUPDATE ACCT_MSTR SET STATUS =‘A’

    WHERE BRANCH NO=’B’ ||vBRANCH_NO AND STATUS =‘S’IF SQL%ROWCQNT> 0 THEN

    DBMS OUTPUT. PUT_LINE(TO_CHAR(sQL%ROWCOUNT)|| ‘ACCOUNTS ACTIVATEDSUCCESSFULLY’);

    ELSE

    DBMS_OUTPUT. PUT_LINE(’CURRENTLY THERE EXIST NO INACTIVE ACCOUNTS IN

    THE BRANCH NO’ || vBRANCH_NO);END IF;

    END;PROCEDURE ACT_ACCTS(vNAME IN VARCHAR2) IS

    BEGIN

    UPDATE ACCT_MSTR SET STATUS= ‘A’ WHERE STATUS =‘S’

    AND BRANCH_NO IN(SELECT BRANCH_NO FROM BRANCH_MSTR WHERE NAME=

    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);

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    27/34

    END IF;

    END;END ACCT_MNTC;

    Output:

    Package body created.

    EXECUTE ACCTMNTC.ACTACCTS(1);

    EXECUTE ACCT_MNTC.ACCT ACCTS(’Vile Parle’);

    P+-(- N/ 8

    A()* C+,-(0 T+(,+ 

    Example1

    Create a transparent audit system for a table CUST_MSTR. The system must keep track of the

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    28/34

    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(

    CUST53OVARCHAR2(20),FNAME VARCHAR2(25),MNAME VARCHAR2(25),

    LNAME VARCHAR2(25),DOB_INC DATE 3OTNULL,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.

    Example2

    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.

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    29/34

    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>5C 3T5=CCT53O

    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

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    30/34

    PRIMARY_KEY_VALUE VARCHAR2(1O);!E7I3

    SELECT ‘C’ || TO_CHAR(CUST_SEQ.NEXTVAL)INTO

    PRIMARY_KEY_VALUE FROM DUAL;

    :NEW.CUST_NO:=PRIMARY_KEY_VALUE;

    END;

    Output:

    Trigger created.

    P+-(- N/. 9

    A()* +(, - +/+-) H- -,, 0-), -0: :(- ( 5 (), (0 / +,,0.

    0EC,=RE

     3=4E >=RC?=R2(1

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    31/34

    P+-(- N/. 1"

    A()* +(, - +/+-) H- -,, +(0(-% :+-(/0 &(0 )/0H'% -0: +-, / (0,+, = --,

    M-+( A)/0

    0EC,=RE

    PRI3CIP=, 3U4!ER :B LPRI3CIP=,@

    4O3T?S 3U4!ER :B L4O3T?S@

    ROI 3U4!ER :B LROI

    4=4OU3T 3U4!ER@

    !E7I3

    4=4OU3T :B PRI3CIP=, N (PRI3CIP=, K ROI-1) K 4O3T?S@

    0!4S5OUTPUT*PUT5,I3E(4=4OU3T)@

    E30

    O *

    PRI3CIP=, B

    ROI B

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    32/34

    P+-(- N/. 11

    A()* +(, - +/+-) H- -,, +-:( = --, A+,- / (+,. U, PI - C/0-0.

    0EC,=RE

    R=0IUS 3U4!ER :B L R=0IUS@

    PI 3U4!ER :B *1@

    =RE= 3U4!ER@

    !E7I3

    =RE=:B PI K (R=0IUS K R=0IUS)@

    0!4S5OUTPUT*PUT5,I3E(‘=RE= IS ‘ DD=RE=)@

    E30

    O *

    R=0IUS B

    =RE= IS

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    33/34

    P+-(- N/. 12

    A()* +(, - +/+-) H- -,, 0),+ = :(- JH,H,+ ( ( , H-0% /+ ,- /% /+ +,-,+

    H-0 1".

    0EC,=RE

     3 3U4!ER :B E3 3U4!ER IS E.U=, TO 1’)@

    E,SE IF 3 M 1 T?E3

    0!4S5OUTPUT*PUT5,I3E(’7I>E3 3U4!ER IS ,ESS T?=3 1’)@

    E,SE IF 3 1 T?E3

    0!4S5OUTPUT*PUT5,I3E(’7I>E3 3U4!ER IS 7RE=TER T?=3 1’)@

    E30 IF@

    E30

  • 8/15/2019 DBMS Practicals Sem 3 Mca Idol | Shree Ram College

    34/34

    O *

    7I>E3 3U4!ER IS ,ESS T?=3 1*