-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStep_5_Final_Academic_Upgrades.sql
More file actions
79 lines (72 loc) · 2.59 KB
/
Copy pathStep_5_Final_Academic_Upgrades.sql
File metadata and controls
79 lines (72 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- ============================================================
-- CORE BANKING SYSTEM (CBS) — PERFORMANCE & VIEW UPGRADES
-- DEVELOPED BY: HUZAIFA IMRAN & MUHAMMAD ARSLAN
-- Phase III & IV: Performance and Advanced PL/SQL
-- ============================================================
-- 1. PERFORMANCE: Indexing (Phase III)
-- Optimizing retrieval for frequently searched identity columns
CREATE INDEX IF NOT EXISTS idx_customers_cnic ON customers(cnic);
CREATE INDEX IF NOT EXISTS idx_customers_email ON customers(email);
CREATE INDEX IF NOT EXISTS idx_customers_phone ON customers(phone);
-- 2. OBJECT-ORIENTED FEATURES: Custom Types (Phase IV)
-- Demonstrating object-oriented database features
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'contact_info') THEN
CREATE TYPE contact_info AS (
email VARCHAR(100),
phone VARCHAR(20)
);
END IF;
END $$;
-- 3. VIEWS: Complex View (Technical Constraints)
-- Simplifies data access for the front-end by joining 3 tables
CREATE OR REPLACE VIEW v_customer_financial_summary AS
SELECT
a.account_id,
c.customer_id,
c.first_name,
c.last_name,
c.first_name || ' ' || c.last_name AS full_name,
c.cnic,
a.account_number,
a.balance,
a.status,
at.type_name,
(SELECT COUNT(*) FROM transactions t WHERE t.account_id = a.account_id) as total_txns
FROM
customers c
JOIN
accounts a ON c.customer_id = a.customer_id
JOIN
account_types at ON a.account_type_id = at.account_type_id;
-- 4. PL/SQL: Explicit Cursor & Calculations (Phase IV)
-- This function uses an explicit cursor to calculate total bank risk
CREATE OR REPLACE FUNCTION calculate_bank_risk_metrics()
RETURNS TABLE(risk_level TEXT, total_exposure NUMERIC) AS $$
DECLARE
curr_acc CURSOR FOR SELECT balance FROM accounts;
acc_bal NUMERIC;
high_risk_sum NUMERIC := 0;
BEGIN
OPEN curr_acc;
LOOP
FETCH curr_acc INTO acc_bal;
EXIT WHEN NOT FOUND;
-- Business logic: High exposure if balance > 500,000
IF acc_bal > 500000 THEN
high_risk_sum := high_risk_sum + acc_bal;
END IF;
END LOOP;
CLOSE curr_acc;
risk_level := 'HIGH EXPOSURE';
total_exposure := high_risk_sum;
RETURN NEXT;
END;
$$ LANGUAGE plpgsql;
-- 5. SET OPERATIONS: UNION Example (Phase III)
-- Used for internal reporting of all entities
CREATE OR REPLACE VIEW v_all_system_entities AS
SELECT first_name || ' ' || last_name as entity_name, 'CUSTOMER' as entity_type FROM customers
UNION
SELECT account_number::text, 'ACCOUNT' as entity_type FROM accounts;