Top Banner
Practical No. 1 AIM: Creation of Banking database create table branch (br_name varchar(15) primary key, br_city varchar(10), asset int) ; create table account (acc_no varchar(10) primary key, br_name varchar(15), balance int, foreign key(br_name) references branch) ; create table depositor (cust_name varchar(15), acc_no varchar(10) 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 char(10)) ;
58
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Idol Dbms Journal 1

Practical No. 1

AIM: Creation of Banking database

create table branch

(br_name varchar(15) primary key,

br_city varchar(10),

asset int) ;

create table account

(acc_no varchar(10) primary key,

br_name varchar(15),

balance int,

foreign key(br_name) references branch) ;

create table depositor

(cust_name varchar(15),

acc_no varchar(10) 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 char(10)) ;

create table borrower

(cust_name varchar(15) foreign key references customer,

Page 2: Idol Dbms Journal 1

loan_no varchar(15) foreign key references loan) ;

create table loan

(loan_no varchar(15) primary key,

br_name varchar(15) foreign key references branch,

amount 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('Glenn','Walnut','Stamford')

insert into customer values('Hayes','Main','Harrison')

insert into customer values('Johnson','Alma','Palo Alto')

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('Williams','Nassau','Princeton')

insert into depositor values('Hayes','A-102')

insert into depositor values('Johnson','A-101')

insert into depositor values('Johnson','A-201')

Page 3: Idol Dbms Journal 1

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

select * from depositor

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','Palo Alto',2100000)

insert into branch values('Round Hill','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-201','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

Page 4: Idol Dbms Journal 1

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',1000)

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;

Page 5: Idol Dbms Journal 1

Practical No. 2

AIM: Queries on Banking database

Tables:

SQL> select * from branch;

BR_NAME BR_CITY ASSETS

-------------------- -------------------- ---------

brighton brooklyn 7100000

downtown brooklyn 9000000

mianus horseneck 400000

north town rye 3700000

perryridge horseneck 1700000

pownal bennington 300000

redwood palo alto 2100000

round hill horseneck 8000000

SQL> select * from account;

ACC_NO BR_NAME BAL

---------- -------------------- ------------------------

A-102 perryridge 400

A-201 brighton 900

A-215 mianus 700

A-217 brighton 750

A-222 redwood 700

A-305 round hill 350

Page 6: Idol Dbms Journal 1

SQL> select * from loan;

L_NO BR_NAME AMT

---------- -------------------- ----------------------

l-11 round hill 900

l-14 downtown 1500

l-15 perryridge 1500

l-16 perryridge 1300

l-17 downtown 1000

l-23 redwood 2000

l-93 mianus 500

SQL> select * from customer;

C_NAME C_STREET C_CITY

-------------------- -------------------- ------------------

adams spring pittfield

brooks sentor brooklyn

curry north rye

glenn sand hill woodside

green walnut stamford

hayes main harrison

johnson alma palo alto

jones main harrison

lindsay park pittsfield

smith north rye

turner putnam stanford

Page 7: Idol Dbms Journal 1

williams nassau Princeton

SQL> select * from depositor;

C_NAME ACC_NO

-------------------- ----------

hayes A-102

johnson A-201

jones A-217

lindsay A-222

smith A-215

turner A-305

SQL> select * from borrower;

C_NAME L_NO

-------------------- ----------

adams l-16

curry l-93

hayes l-15

johnson l-14

jones l-17

smith l-11

smith l-23

williams l-17

Page 8: Idol Dbms Journal 1

Queries:

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

SQL> select distinct br_name

2 from loan;

Output:

BR_NAME

--------------------

downtown

mianus

perryridge

redwood

round hill

2)Find all loan numbers for loans made at the perryridge branch with loan amount greater than 1200.

SQL> select l_no

2 from loan

3 where br_name='perryridge' and amt>1200;

Output:

L_NO

----------

l-15

l-16

Page 9: Idol Dbms Journal 1

3)Find the loan number with loan amounts between 90000 and 100000.

SQL> select l_no

2 from loan

3 where amt between 90000 and 100000;

Output:

no rows selected

4)Find all customers who have a loan from the bank, Find their names,loan numbers and loan amount.

SQL> select c_name,b.l_no,l.amt

2 from borrower b,loan l

3 where b.l_no=l.l_no;

Output:

C_NAME L_NO AMT

-------------------- ---------- ---------

adams l-16 1300

curry l-93 500

hayes l-15 1500

johnson l-14 1500

jones l-17 1000

smith l-11 900

smith l-23 2000

williams l-17 1000

Page 10: Idol Dbms Journal 1

5)Find the customer names,loan numbers and loan amounts for all loans at th perryridge branch.

SQL> select c_name,b.l_no,amt

2 from borrower b,loan l

3 where b.l_no=l.l_no and br_name='perryridge';

Output:

C_NAME L_NO AMT

-------------------- ---------- ---------

adams l-16 1300

hayes l-15 1500

6)Find the names of all branches that have assets greater than at least one branch located in Brooklyn.

SQL> select distinct t.br_name

2 from branch t,branch s

3 where t.assets>s.assets and s.br_city='brooklyn';

Output:

BR_NAME

--------------------

downtown

round hill

Page 11: Idol Dbms Journal 1

7)Find the names of all customers whose street address includes the substring ‘main’.

SQL> select c_name

2 from customer

3 where c_street like '%main%';

Output:

C_NAME

--------------------

hayes

jones

8)Find in alphabetic order all customers who have a loan at the Perryridge branch.

SQL> select distinct c_name

2 from borrower b,loan l

3 where b.l_no=l.l_no and

4 br_name='perryridge'

5 Order by c_name;

Output:

C_NAME

--------------------

hayes

Page 12: Idol Dbms Journal 1

9)Find all customers having a loan ,an account or both at the bank.

SQL> select c_name

2 from depositor

3 union

4 select c_name

5 from borrower;

Output:

C_NAME

--------------------

curry

hayes

johnson

jones

lindsay

smith

turner

williams

10)Find all customers who have both a loan and an account at the bank.

SQL> select c_name

2 from depositor

3 intersect

4 select c_name

5 from borrower;

Page 13: Idol Dbms Journal 1

Output:

C_NAME

--------------------

hayes

johnson

jones

smith

11)Find the average account balance at each branch.

SQL> select br_name,avg(bal)

2 from account

3 group by br_name;

Output:

BR_NAME AVG(BAL)

-------------------- ---------

brighton 825

mianus 700

perryridge 400

redwood 700

round hill 350

Page 14: Idol Dbms Journal 1

12)Find the number of depositors for each branch.

SQL> select br_name,count(distinct c_name)

2 from depositor d,account a

3 where d.acc_no=a.acc_no

4 group by br_name;

Output:

BR_NAME COUNT(DISTINCTC_NAME)

-------------------- ---------------------

brighton 2

mianus 1

perryridge 1

redwood 1

round hill 1

13)Find the branch name whose average balance is greater than 1200.

SQL> select br_name,avg(bal)

2 from account

3 group by br_name

4 having avg(bal)>1200;

Output:

no rows selected

Page 15: Idol Dbms Journal 1

14)Find the average balance for each customer who lives in Harrison and has at least three accounts.

SQL> select d.c_name,avg(bal)

2 from depositor d,account a,customer c

3 where d.acc_no=a.acc_no and d.c_name=c.c_name and

4 c_city='harrison'

5 group by d.c_name

6 having count(distinct d.acc_no)>=3;

Output:

no rows selected

15)Find the loan numbers in the loan relation with null values for amount.

SQL> select l_no

2 from loan

3 where amt is null;

Output:

no rows selected

16)Find those customers who are borrowers from bank.

SQL> select distinct c_name

2 from borrower

3 where c_name in(select c_name from depositor);

Page 16: Idol Dbms Journal 1

Output:

C_NAME

--------------------

hayes

johnson

jones

smith

17)Find all customers who have both an account and a loan at the perryridge branch.

SQL> select distinct c_name

2 from borrower b,loan l

3 where b.l_no=l.l_no and br_name='perryridge' and

4 (br_name,c_name)in

5 (select br_name,c_name

6 from depositor d,account a

7 where d.acc_no=a.acc_no);

Output:

C_NAME

--------------------

hayes

Page 17: Idol Dbms Journal 1

18)Find all customers who do have a loan at the bank,but do not have an account at the bank.

SQL> select distinct c_name

2 from borrower

3 where c_name not in(select c_name from depositor);

Output:

C_NAME

--------------------

curry

williams

19)Find the names of all branches that have assets greater than those of at least one branch located in Brooklyn.

SQL> select br_name

2 from branch

3 where assets>some(select assets from branch

4 where br_city='brooklyn');

Output:

BR_NAME

--------------------

downtown

round hill

Page 18: Idol Dbms Journal 1

20)Find the branch that has the highest average balance.

SQL> select br_name

2 from account

3 group by br_name

4 having avg(bal)>=all(select avg(bal) from account

5 group by br_name);

Output:

BR_NAME

--------------------

brighton

Page 19: Idol Dbms Journal 1

Practical No. 3

AIM: Creation of Supplier-Parts-Catalog database and Queries on it.

Tables:

SQL> select * from suppliers;

SID SNAME ADDRESS

--------- -------------------- --------------------

101 nipa malad

102 vidita borivali

103 vashita malad

104 poonam kandivali

105 samrat 221 spacepackers

106 girish chikuwadi

SQL> select * from parts;

PID PNAME COLOR

--------- -------------------- -----------------

1 monitor red

2 keyboard green

3 mouse green

4 monitor white

5 headphones black

6 cpu red

Page 20: Idol Dbms Journal 1

SQL> select * from catalog;

SID PID COST

--------- --------- ---------

101 1 10000

102 2 15000

103 3 1000

104 4 7000

105 5 15000

106 6 19000

101 2 1000

101 3 1000

101 4 7000

101 5 15000

101 6 19000

Page 21: Idol Dbms Journal 1

Queries:

1)Find the names of suppliers who supply some red part.

SQL> select sname

2 from suppliers,parts,catalog

3 where parts.pid=catalog.pid and

4 suppliers.sid=catalog.sid and

5 parts.color='red';

Output:

SNAME

--------------------

nipa

girish

Page 22: Idol Dbms Journal 1

2)Find the sid of suppliers who supply some red or green part.

SQL> select distinct catalog.sid

2 from suppliers,catalog,parts

3 where suppliers.sid=catalog.sid and

4 parts.pid=catalog.pid and

5 parts.color='green' union select distinct catalog.sid

6 from suppliers,catalog,parts

7 where suppliers.sid=catalog.sid and

8 parts.pid=catalog.pid and

9 parts.color='red';

Output:

SID

-------------

101

102

103

106

Page 23: Idol Dbms Journal 1

3)Find the sid of suppliers who supply some red part or are at 221 space packers street.

SQL> select distinct catalog.sid from catalog,suppliers,parts

2 where suppliers.sid=catalog.sid and parts.pid=catalog.pid and

3 parts.color='red' union select distinct catalog.sid

4 from suppliers,catalog,parts

5 where suppliers.sid=catalog.sid and parts.pid=catalog.pid and

6 suppliers.address='221 spacepackers';

Output:

SID

---------

101

105

106

4)Find the sid of suppliers who supply some red part and some green part.

SQL> select distinct catalog.sid from suppliers,catalog,parts

2 where suppliers.sid=catalog.sid and parts.pid=catalog.pid and

3 parts.color='green' intersect select distinct catalog.sid

4 from suppliers,catalog,parts

5 where suppliers.sid=catalog.sid and parts.pid=catalog.pid and

6 parts.color='red';

Output:

SID

-------------

101

Page 24: Idol Dbms Journal 1

5)Find the sid of suppliers who supply every part.

SQL> select c.sid

2 from catalog c

3 where not exists(select p.pid from parts p

4 where not exists(select c1.sid from catalog c1

5 where p.pid=c1.pid

6 and c1.sid=c.sid));

Output:

SID

------------

101

101

101

101

101

101

6) Find the sid of suppliers who supply every red part.

SQL> select c.sid

2 from catalog c

3 where not exists(select p.pid from parts p

4 where p.color='red' and not exists(select c1.sid from catalog c1

5 where p.pid=c1.pid

6 and c1.sid=c.sid));

Page 25: Idol Dbms Journal 1

Output:

SID

------------

101

101

101

101

101

101

7)Find the pairs of sid such that the suppliers with the first sid charges more for some part then the suppliers with the second sid.

SQL> select c1.sid,c2.sid

2 from catalog c1,catalog c2

3 where c1.pid=c2.pid and

4 c1.sid!=c2.sid and

5 c1.cost>c2.cost;

Output:

SID SID

--------- -------------

102 101

Page 26: Idol Dbms Journal 1

8)Find the pid of parts supplied by atleast 2 different suppliers.

SQL> select distinct c.pid

2 from catalog c

3 where exists(select c1.sid

4 from catalog c1

5 where c1.pid=c.pid and c1.sid !=c.sid);

Output:

PID

---------

2

3

4

5

6

Page 27: Idol Dbms Journal 1

9)Find the pid’s of most expensive part supplied by supplier name Vidita.

SQL> select pid

2 from catalog c1,suppliers s1

3 where c1.sid=s1.sid

4 and s1.sname='vidita' and

5 c1.cost>=all(select c2.cost from catalog c2,suppliers s2 where c2.sid=s2.sid and

6 s2.sname='vidita');

Output:

PID

---------

2

Page 28: Idol Dbms Journal 1

Practical No. 4

AIM: Creation of flight database and queries on it.

Tables:

SQL> select * from flights;

FLNO FRM TOO DISTANCE DEPARTS ARRIVES

--------- -------------------- -------------------- --------- ---------- ---------------------------------

111 bonn madras 700 12:20 3:20

222 mumbai chennai 800 1:20 3:20

333 goa mumbai 550 18:20 19:20

444 chennai mumbai 800 5:20 7:20

555 delhi mumbai 900 20:30 22:20

666 surat patna 800 2:20 4:20

SQL> select * from aircraft;

AID ANAME CRUISINGRANGE

--------- -------------------- -------------

101 boeing 4000

102 air india 1000

103 spicejet 2000

104 indigo 5000

105 kingfisher 6000

106 jet airways 300

Page 29: Idol Dbms Journal 1

SQL> select * from employees;

EID ENAME SALARY

--------- -------------------- ---------

1 vidita 1000000

2 varada 100000

3 kali 20000

4 komal 10000

5 urvi 50000

6 ruchi 30000

SQL> select * from certified;

EID AID

--------- ---------

1 101

1 102

1 103

2 104

3 105

4 106

5 102

6 104

Page 30: Idol Dbms Journal 1

Queries:

1)Find the eid’s of pilots certified for some boeing aircraft.

SQL> select eid

2 from aircraft a,certified c

3 where a.aid=c.aid and

4 aname='boeing';

Output:

EID

---------

1

2)Find the names of pilots certified for some boeing aircraft.

SQL> select ename

2 from aircraft a,certified c,employees e

3 where c.aid=a.aid and

4 e.eid=c.eid and aname='boeing';

Output:

ENAME

--------------------

vidita

Page 31: Idol Dbms Journal 1

3)Find the aid of all aircraft that can be used on nonstop flights from bonn to madras.

SQL> select a.aid

2 from aircraft a,flights f

3 where f.frm='bonn' and f.too='madras' and

4 f.distance < a.cruisingrange;

Output:

AID

---------

101

102

103

104

105

4)Find the names of pilots who can obtain planes with the range greater than 3000 miles but are not certified for any boeing aircraft.

SQL> select e.ename

2 from employees e,aircraft a,certified c

3 where e.eid=c.eid and a.aid=c.aid and

4 cruisingrange>3000 and

5 e.eid not in(select c1.eid

6 from certified c1,aircraft a1

7 where c1.aid=a1.aid and

8 aname='boeing');

Page 32: Idol Dbms Journal 1

Output:

ENAME

--------------------

varada

kali

ruchi

5)Find the eid’s of employees who make the highest salary.

SQL> select e1.eid from employees e1

2 where e1.salary=(select max(e2.salary)from employees e2);

Output:

EID

---------

1

6)Find the eid’s of employees who make the second highest salary.

SQL> select e.eid from employees e

2 where e.salary=(select max(e1.salary)from employees e1

3 where e1.salary !=(select max(e2.salary)from employees e2));

Output:

EID

------------

2

Page 33: Idol Dbms Journal 1

7)Find the eid of employees who are certified for the largest number of aircraft.

SQL> create view temp as

2 (select c.eid as eid,count(c.aid) as cnt

3 from certified c

4 group by(c.eid));

View created.

SQL> select temp.eid

2 from temp where temp.cnt=(select max(cnt) from temp);

Output:

EID

-----------

1

8)Find the eid’s of employees who are certified for exactly 3 aircraft.

SQL> select temp.eid from(select c.eid as eid,count(c.aid) as cnt

2 from certified c

3 group by(c.eid))temp

4 where temp.cnt=3;

Output:

EID

---------

1

Page 34: Idol Dbms Journal 1

9)Find the total amount paid to employees as salaries.

SQL> select sum(salary) as sum_sal

2 from employees;

Output:

SUM_SAL

---------

1210000

10)Identify the flights that can be piloted by every pilot whose salary is more than $100000.

SQL> select distinct e.ename

2 from aircraft a,certified c,employees e,flights f

3 where a.aid=c.aid and e.eid=c.eid and

4 distance<cruisingrange and salary>100000;

Output:

ENAME

--------------------

vidita

Page 35: Idol Dbms Journal 1

Practical No. 5

AIM: Creation of Employee database

Consider the employee database of Figure (a), where the primary keys are underlined.

Give an expression in SQL for each of the following queries.

employee (employee-name, street, city)

works (employee-name, company-name, salary)

company (company-name, city)

manages (employee-name, manager-name)

Figure (a) Employee database.

a. Find the names of all employees who work for First Bank Corporation.

select employee-name

from works

where company-name = ’First Bank Corporation’

b. Find the names and cities of residence of all employees who work for First

Bank Corporation.

select e.employee-name, city

from employee e, works w

where w.company-name = ’First Bank Corporation’ and

w.employee-name = e.employee-name

Page 36: Idol Dbms Journal 1

c. Find the names, street address, and cities of residence of all employees who

work for First Bank Corporation and earn more than $10,000.

select *

from employee

where employee-name in

(select employee-name

from works

where company-name = ’First Bank Corporation’ and salary > 10000)

d. Find all employees in the database who live in the same cities as the companies

for which they work.

select e.employee-name

from employee e, works w, company c

where e.employee-name = w.employee-name and e.city = c.city and

w.company -name = c.company –name

e. Find all employees in the database who live in the same cities and on the

same streets as do their managers.

select P.employee-name

from employee P, employee R, manages M

where P.employee-name = M.employee-name and

M.manager-name = R.employee-name and

Page 37: Idol Dbms Journal 1

P.street = R.street and P.city = R.city

f. Find all employees in the database who do not work for First Bank Corporation.

select employee-name

from works

where company-name != ’First Bank Corporation’

OR

select employee-name

from employee

where employee-name not in

(select employee-name

from works

where company-name = ’First Bank Corporation’)

g. Find all employees in the database who earn more than every employee of

Small Bank Corporation.

select employee-name

from works

where salary > all

(select salary

from works

Page 38: Idol Dbms Journal 1

where company-name = ’Small Bank Corporation’)

h. Find all employees who earn more than the average salary of all employees

of their company.

select employee-name

from works T

where salary > (select avg (salary)

from works S

where T.company-name = S.company-name)

i. Find the company that has the most employees.

select company-name

from works

group by company-name

having count (distinct employee-name) >= all

(select count (distinct employee-name)

from works

group by company-name)

j. Find the company that has the smallest payroll.

Page 39: Idol Dbms Journal 1

select company-name

from works

group by company-name

having sum (salary) <= all (select sum (salary)

from works

group by company-name)

k. Find those companies whose employees earn a higher salary, on average,

than the average salary at First Bank Corporation.

select company-name

from works

group by company-name

having avg (salary) > (select avg (salary)

from works

where company-name = ’First Bank Corporation’)

l. Modify the database so that Jones now lives in Newtown.

update employee

set city = ’Newton’

where person-name = ’Jones’

m. Give all employees of First Bank Corporation a 10-percent raise.

Page 40: Idol Dbms Journal 1

update works

set salary = salary * 1.1

where company-name = ’First Bank Corporation’

n. Give all managers of First Bank Corporation a 10-percent raise.

update works

set salary = salary * 1.1

where employee-name in (select manager-name

from manages)

and company-name = ’First Bank Corporation’

o. Delete all tuples in the works relation for employees of Small Bank Corporation.

delete works

where company-name = ’Small Bank Corporation’

Practical No. 6

Page 41: Idol Dbms Journal 1

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 statement for the AUDIT_CUST table:

CREATE TABLE AUDIT_CUST(

CUST_NO VARCHAR2(10),FNAME VARCHAR2(25),MNAME VARCHAR2(25),

LNAME VARCHAR2(25), DOB_lNC DATE NOT NULL,OCCUP VARCHAR2(25),PHOTOGRAPH VARCHAR2(25), SIGNATURE VARCHAR2(25), PANCOPY VARCHAR2(1),FORM60 VARCHAR2(1), OPERATION VARCHAR2(20),USERID VARCHAR2(20),ODATE DATE);

Output:

Table created.

CREATE TRIGGER AUDIT_TRAIL

AFTER UPDATE OR DELETE ON CUST_MSTR

FOR EACH ROW

DECLARE

OPER VARCHAR2(8);

BEGIN

Page 42: Idol Dbms Journal 1

IF updating THEN

OPER := 'UPDATE';

END IF;

IF deleting THEN

OPER := 'DELETE';

END IF;

INSERT INTO AUDIT_CUST VALUES (:OLD.CUST_NO, :OLD.FNAME, :OLD.MNAME,:OLD.LNAME,. :OLD.D0B_INC, :OLD.OCCUP, :OLD.PHOTOGRAPH,:OLD.SIGNATURE, :OLD.PANCOPY, :OLD.FORM60, OPER, USER, SYSDATE);

END;

Output:

Trigger created.

Example 2

Write a database 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 FOR EACH ROW

DECLARE

v_CNT_ACCT_NO VARCHAR2(10);

v_CURBAL NUMBER(10);

BEGIN

SELECT COUNT(ACCT_NO) INTO v_CNT_ACCT_NO

Page 43: Idol Dbms Journal 1

FROM ACCT_MSTR WHERE ACCT_NO = :new.ACCT_NO;

IF v_CNT_ACCT_NO = 0 THEN

RAISE_APPLICATlON_ERROR( -20000,'The Account number is invalid.');

END IF;

IF :new.AMT <= 0 THEN

RAISE_APPLICATlON_ERROR(-20001,'The Transaction amount cannot' be negative or zero.');

END IF;

SELECT CURBAL INTO v_CURBAL

FROM ACCT_MSTR WHERE ACCT_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

Page 44: Idol Dbms Journal 1

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

BEGIN

SELECT ‘C’||TO_CHAR(CUST_SEQ.NEXTVAL) INTO PRIMARY_KEY_VALUE FROM DUAL;

:NEW.CUST_NO:=PRIMARY_KEY_VALUE;

END;

Output:

Trigger created