Banking Scenario Procedures: ER Diagram, SQL Commands, Procedures

Verified

Added on  2019/10/18

|9
|1874
|471
Practical Assignment
AI Summary
This assignment presents a comprehensive solution to a banking scenario, covering various aspects of database design and implementation. It begins with an Entity-Relationship (ER) diagram and then provides the necessary SQL commands to create and populate tables for customers, accounts, and transactions. The core of the assignment focuses on SQL procedures designed to handle common banking operations. These procedures include adding new customers (with validation for duplicates and valid types), adding new accounts for existing customers (with validation for customer and account types), handling transactions (deposits, withdrawals, and transfers) while maintaining account balance integrity (implemented via a trigger to prevent negative balances), displaying the sum of balances by state, updating customer addresses, and displaying transaction history based on amount. The assignment also includes an extra credit component that involves creating a trigger to log customer address changes. The solution demonstrates a practical application of database concepts, including data validation, stored procedures, triggers, and data manipulation, within the context of a banking system.
Document Page
BANKING SCENARIO PROCEDURES
STUDENT
08 OCTOBER, 2016
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Contents
ER DIAGRAM...........................................................................................................................2
CREATE AND INSERT TABLE COMMANDS....................................................................2
SQL PROCEDURES.................................................................................................................3
1 | P a g e
Document Page
ER DIAGRAM
As mentioned in the mail by you, that you have already drawn the ERD. So ERD is not
missing. You can paste the ERD in this area.
CREATE AND INSERT TABLE COMMANDS
CREATE SEQUENCE c1
MINVALUE 1
MAXVALUE 999999
START WITH 1
INCREMENT BY 1
CACHE 20;
CREATE SEQUENCE T1
MINVALUE 1
MAXVALUE 999999
START WITH 1
INCREMENT BY 1
CACHE 20;
create table customer
(
cust_id int primary key,
cust_type varchar2(20),
fname varchar2(20),
lname varchar2(20),
street varchar2(30),
city varchar2(30),
zip number(6,0),
state varchar2(30),
gender char(1),
constraint chk_1 check (cust_type IN ('small business', 'large business', 'personal')),
constraint chk_2 check (gender IN ('M', 'F'))
);
INSERT INTO CUSTOMER VALUES (C1.NEXTVAL, 'small business', 'JIA', 'STEVEN',
'21 PARK STREET', 'ASPEN', 889988, 'CA', 'F');
INSERT INTO CUSTOMER VALUES (C1.NEXTVAL, 'large business', 'DIA', 'STONES',
'212 PARK VIEW ROAD', 'ASPEN', 889908, 'CA', 'F');
INSERT INTO CUSTOMER VALUES (C1.NEXTVAL, 'personal', 'LIA', 'DAVID', '41
GREEN PARK STREET', 'ASPEN', 889918, 'CA', 'F');
create table account
( acct_id int primary key,
cust_id int,
acct_type varchar2(20) not null,
acct_num number(16,0)unique ,
balance number(10,2),
constraint fk_1 foreign key(cust_id) references customer(cust_id),
2 | P a g e
Document Page
constraint chk22 CHECK (acct_type IN ('savings', 'checking' , 'business'))
);
INSERT INTO ACCOUNT VALUES (1, 1, 'savings', 567890000007890, 1500.90);
INSERT INTO ACCOUNT VALUES (2, 2, 'savings', 567890000009990, 2000.90);
INSERT INTO ACCOUNT VALUES (3, 3, 'checking', 567890000001090, 15000.90);
create table transaction
(
transc_id int primary key,
acct_id int,
amount number(10,2),
trans_date date,
trans_type varchar(20),
constraint fk2 foreign key(acct_id) references account(acct_id),
constraint chk_t1 CHECK (trans_type IN ('deposit', 'withdrawal', 'transfer'))
);
INSERT INTO TRANSACTION VALUES (T1.NEXTVAL, 1, 678.00, '10-11-2015',
'deposit');
INSERT INTO TRANSACTION VALUES (T1.NEXTVAL, 2, 118.00, '11-11-2015',
'withdrawal');
INSERT INTO TRANSACTION VALUES (T1.NEXTVAL, 3, 500.00, '12-11-2015',
'deposit');
SQL PROCEDURES
1. (Using a procedure) System should be able to add new customers ensuring that no duplicates
are entered, and type is valid. A message should be returned with the success or failure of the
action.
Answer:
CREATE OR REPLACE PROCEDURE add_new_customer
(
input_type customer.cust_type%type,
input_fname CUSTOMER.fname%type,
input_lname CUSTOMER.lname%type,
input_addr CUSTOMER.STREET%type,
input_city CUSTOMER.CITY%type,
input_zip CUSTOMER.ZIP%type,
input_state CUSTOMER.STATE%type,
input_gender CUSTOMER.GENDER%type
)
IS
record_count NUMBER;
duplicate_exception EXCEPTION;
invalid_type_exception EXCEPTION;
CURSOR customer_exists IS
SELECT COUNT (*)
3 | P a g e
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
FROM customer
WHERE UPPER (input_fname) = UPPER(fname)
AND UPPER(input_lname) = UPPER(lname)
AND UPPER(input_addr) = UPPER(street)
AND UPPER(input_city) = UPPER(CITY)
AND input_zip = ZIP
AND UPPER (input_state) = UPPER(STATE)
AND UPPER (input_gender) = UPPER(GENDER)
AND lower(input_type) = cust_type;
BEGIN
IF (input_type not in ('small business', 'large business', 'personal') and input_type is not null) THEN
raise invalid_type_exception;
END IF;
record_count := 0;
OPEN customer_exists;
FETCH customer_exists INTO record_count;
CLOSE customer_exists;
IF record_count > 0 THEN
RAISE duplicate_exception;
END IF;
INSERT INTO customer
(CUST_ID,cust_type, FNAME, LNAME, STREET, CITY, ZIP, STATE, GENDER)
VALUES
(c1.nextval,input_type, input_fname,
input_lname,input_addr,input_city,input_zip,input_state,input_gender);
COMMIT;
dbms_output.put_line ('New Customer inserted');
EXCEPTION
WHEN duplicate_exception THEN
dbms_output.put_line ('Customer exists already. Please try again');
WHEN invalid_type_exception THEN
dbms_output.put_line ('Invalid type entered. Please try again');
WHEN OTHERS THEN
dbms_output.put_line ('Transaction Failed, customer not inserted ');
END add_new_customer;
1. (Using a procedure) System should be able to add a new account for an existing customer.
Given a customer ID, and an account type, the system should verify that the customer id, and
account type are valid, then create a new account. A message should be returned with the
success or failure of the action.
CREATE OR REPLACE PROCEDURE add_new_account
(
4 | P a g e
Document Page
input_cid account.cust_id%type,
input_actype account.acct_type%type,
input_acctid account.acct_id%type,
input_num account.acct_num%type,
input_bal account.balance%type
)
IS
not_found_exception EXCEPTION;
invalid_actype_exception EXCEPTION;
cnumber number;
cursor c1 is
SELECT cust_id
FROM customer
WHERE cust_id = input_cid;
BEGIN
IF (input_actype not in ('savings', 'checking', 'business') and input_actype is not null) THEN
raise invalid_actype_exception;
END IF;
open c1;
fetch c1 into cnumber;
if c1%notfound then
raise not_found_exception;
END IF;
INSERT INTO account (acct_id , cust_id, acct_type, acct_num, balance) VALUES
(input_acctid , input_cid, input_actype, input_num, input_bal);
COMMIT;
dbms_output.put_line ('New account inserted');
EXCEPTION
WHEN not_found_exception THEN
dbms_output.put_line ('Customer data not found. Please try again');
WHEN invalid_actype_exception THEN
dbms_output.put_line ('Invalid account type entered. Please try again');
WHEN OTHERS THEN
dbms_output.put_line ('Transaction Failed, data not inserted ');
END add_new_account;
2. (Using a procedure and trigger) System should be able to handle valid transactions on valid
accounts for valid customers maintaining the integrity of the account balances. Specifically,
5 | P a g e
Document Page
the system should allow deposits to accounts (must have account number, amount),
withdrawals (must have account number, amount) from accounts and transfers between
accounts (must have 2 account numbers and an amount). When these transactions are made,
the account balance must be updated (via trigger). No account balance may go below zero. A
message should be returned with the success or failure of the action.
3. (Using a procedure) Given a state, display the sum of the balances by state.
CREATE OR REPLACE PROCEDURE state_balance
(
input_states customer.state%type
)
IS
not_found_exception EXCEPTION;
cstate varchar2(30);
balances number(10,2);
cursor c1 is
SELECT state
FROM customer
WHERE state= input_states;
BEGIN
open c1;
fetch c1 into cstate;
if c1%notfound then
raise not_found_exception;
else
select sum(a.balance) into balances from account a, customer c where c.cust_id = a.cust_id
and c.state = cstate;
COMMIT;
dbms_output.put_line ('the balance is: '||balances);
END IF;
EXCEPTION
WHEN not_found_exception THEN
dbms_output.put_line ('state not found. Please try again');
WHEN OTHERS THEN
dbms_output.put_line ('Transaction Failed, enter a valid state');
END state_balance;
4. (Using a procedure) Given a customer id and a new address, update the customer’s address.
A message should be returned with the success or failure of the action.
CREATE OR REPLACE PROCEDURE address_update
(
6 | P a g e
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
input_id customer.cust_id%type,
input_addr CUSTOMER.STREET%type,
input_city CUSTOMER.CITY%type,
input_zip CUSTOMER.ZIP%type,
input_state CUSTOMER.STATE%type
)
IS
cid int;
not_found_cust_exception EXCEPTION;
CURSOR customer_exists IS
SELECT cust_id
FROM customer
WHERE cust_id = input_id;
BEGIN
OPEN customer_exists;
FETCH customer_exists INTO cid;
IF customer_exists%notfound THEN
RAISE not_found_cust_exception;
END IF;
update customer set street = input_addr, city = input_city,zip = input_zip, state = input_state where
cust_id = cid;
COMMIT;
dbms_output.put_line ('New Customer address updated');
EXCEPTION
WHEN not_found_cust_exception THEN
dbms_output.put_line ('data not found for update. Please try again');
WHEN OTHERS THEN
dbms_output.put_line ('Transaction Failed, customer not found ');
END address_update;
5. (Using a procedure) Given an amount, display the account number, last name, and amount of
all transactions with an amount greater than the one passed into the procedure.
CREATE OR REPLACE PROCEDURE transac_history
(
input_amt transaction.amount%type
)
IS
acnum account.acct_num%type;
lastname customer.lname%type;
amts transaction.amount%type;
7 | P a g e
Document Page
cursor c1 is
SELECT a.acct_num, c.lname, t.amount from account a, customer c , transaction t where
a.cust_id = c.cust_id and t.acct_id = a.acct_id and t.amount > = input_amt;
BEGIN
open c1;
loop
fetch c1 into acnum, lastname, amts;
dbms_output.put_line ('account number: ' ||acnum);
dbms_output.put_line ('last name : ' ||lastname);
dbms_output.put_line ('amount: ' ||amts);
end loop;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line ('Transaction Failed, data not inserted ');
END transac_history;
6. [EXTRA CREDIT]: Write a trigger so that anytime a customer changes their address, the old
address, new address, date, customer id and user ID are stored in a log.
Create table cust_log
(cust_id int, ostreet varchar2(20), ocity varchar2(20), ozip number(10,0), ostate varchar(30), nstreet
varchar(20), ncity varchar(20), nzip number(10,2), nstate varchar(30), udate date);
create or replace trigger customer_update
AFTER UPDATE
ON CUSTOMER
FOR EACH ROW
BEGIN
insert into cust_log
(cust_id , ostreet, ocity, ozip, ostate, nstreet, ncity, nzip , nstate , udate)
values
(cust_id, :old.street, :old.city, :old.zip, :old.state, :new.street, :new.city, :new.zip, :new.state,
SYSDATE);
end;
/
8 | P a g e
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]