0% found this document useful (0 votes)
88 views14 pages

Relational Tables For Banking System v1.4

The document describes the design of a banking system database using SQL. It includes: 1) Entity relationship diagrams and normalized relational tables for banks, branches, assets, employees, customers, accounts, account types, loans, and payments. 2) SQL code to create tables, define relationships, insert sample data, and create views and stored procedures to query the data. 3) A trigger to log changes to the account table to an audit table.

Uploaded by

Biniyam Demeke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views14 pages

Relational Tables For Banking System v1.4

The document describes the design of a banking system database using SQL. It includes: 1) Entity relationship diagrams and normalized relational tables for banks, branches, assets, employees, customers, accounts, account types, loans, and payments. 2) SQL code to create tables, define relationships, insert sample data, and create views and stored procedures to query the data. 3) A trigger to log changes to the account table to an audit table.

Uploaded by

Biniyam Demeke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Banking System – ABC’s Bank

1. ER Diagram

2. Relational Tables normalized until third


normalization form

Bank Entity

Bank_Code Bank_Name
100 ABC
Branch Entity

Branch_Name City Bank_Code


BR_01 Addis Ababa 100
BR_02 Adama 100
BR_03 Hawassa 100

Asset Entity

Asset_ID Asset_Name Branch_Name


AS_01 Chair BR_01
AS_02 Table BR_01
AS_03 Computer BR_02
AS_04 Chair BR_03

Employee Entity

Employee_ID Emp_Name Branch_Name Manager_ID Start_Date Emp_Length Telephone


Emp_01 Alemu BR_01 - Jun 15, 3 years 0911223344
Kebede 2020
Emp_09 Debebe BR_01 Emp_01 Jun 15, 2 years 0922334455
Fikadu 2021

Customer Entity

Customer_ID Customer_Name Branch_Name Personal Street &


Banker City
CU_01 Abebe Tesema BR_01 Emp_09 Arada
Street,
AA
CU_02 Solomon Mesfin BR_01 Emp_09 Bole
Street,
AA

Account Entity

Acc_Number Balance Date_Accesse Account_Type Customer_ID


d
ACC_01 150,000.00 May 6, 2022 Saving CU_01
ACC_02 2,000,000.00 Feb 10, 2022 Checking CU_02
Account Type Entity

Account_Typ Attribute
e
Saving Interest Rate
Checking Overdrafts

Loan Entity

Loan_ID Amount CU_ID


LO_01 1,000,000.00 CU_01

Payment Entity

Payment_No. Payment_Amoun Payment_Date Loan_ID


t
Pay_01 10,000.00 Sep 10, 2022 LO_01
Pay_02 12,000.00 Oct 10, 2022 LO_01
Pay_03 8,000.00 Nov 10, 2022 LO_01
Pay_04 9,000.00 Dec 10, 2022 LO_01
Pay_05 11,000.00 Jan 10, 2023 LO_01

3.1. Create Tables on SQL Database


use ABC_Bank_DBMS;

Create table Bank

Bank_Code varchar(255) not null,

Bank_Name varchar(255),

primary key (Bank_Code)

);
CREATE TABLE Branch (

Branch_Name varchar(255) not null,

City varchar(255),

Bank_Code varchar(255) not null,

primary key (Branch_Name)

);

CREATE TABLE Asset (

Asset_ID varchar(255) not null,

Asset_Name varchar(255),

Branch_Name varchar(255),

Primary key (Asset_ID)

);

CREATE TABLE Employee (

Emp_ID varchar(255) not null,

Emp_Name varchar(255),

Branch_Name varchar(255),

Manager_ID varchar(255),

Emp_Date date,

Emp_Length int,

Telephone varchar(30),

Primary Key (Emp_ID)

);
CREATE TABLE Customer (

CU_ID varchar(255) not null,

Customer_Name varchar(255),

Branch_Name varchar(255),

Personal_Banker varchar(255),

Street_City varchar(255),

Primary Key (CU_ID)

);

CREATE TABLE Account (

Account_Number varchar(255) not null,

Balance int,

Date_Accessed date,

Account_Type varchar(255),

CU_ID varchar(255),

Primary Key (Account_Number)

);

CREATE TABLE Account_Type (

Account_Type varchar(255) not null,

Attribute varchar(255),

Primary Key (Account_Type)

);
CREATE TABLE Loan (

Loan_ID varchar(255) not null,

Loan_Amount int,

CU_ID varchar(255),

Primary Key (Loan_ID)

);

CREATE TABLE Payment (

Payment_Number varchar(255) not null,

Payment_Amount int,

Payment_Date date,

Loan_ID varchar(255),

Primary Key (Payment_Number)

);

3.2. Relationship between tables


use ABC_Bank_DBMS;
alter table Branch

Add foreign key (Bank_Code) references Bank (Bank_Code)

GO

alter table Asset

Add foreign key (Branch_Name) references Branch (Branch_Name)

GO

alter table Employee

Add foreign key (Branch_Name) references Branch (Branch_Name)

GO

alter table Employee

Add foreign key (Manager_ID) references Employee (Emp_ID)

GO

alter table Customer

Add foreign key (Branch_Name) references Branch (Branch_Name)

GO

alter table Customer

Add foreign key (Personal_Banker) references Employee (Emp_ID)

GO

alter table Account

Add foreign key (CU_ID) references Customer (CU_ID)

GO
alter table Account

Add foreign key (Account_Type) references Account_Type (Account_Type)

GO

alter table Loan

Add foreign key (CU_ID) references Customer (CU_ID)

GO

alter table Payment

Add foreign key (Loan_ID) references Loan (Loan_ID)

GO

3.3. Inserting Data to the tables and checking the data


with SQL Query

Bank Table
use ABC_Bank_DBMS;

Insert Into Bank


Values ('Bank_01', 'ABC Bank');

Select * from Bank

Branch Table
use ABC_Bank_DBMS;

Insert Into Branch


Values ('BR_01', 'Addis Ababa', 'Bank_01'), ('BR_02', 'Adama', 'Bank_01'), ('BR_03',
'Hawassa', 'Bank_01');

Select * from Branch

Asset Table
use ABC_Bank_DBMS;

Insert Into Asset


Values ('AS_01', 'Chair', 'BR_01'), ('AS_02', 'Table', 'BR_01'), ('AS_03', 'Computer',
'BR_02'), ('AS_04', 'Chair', 'BR_01');

Select * from Asset

Employee Table
use ABC_Bank_DBMS;

Insert Into Employee


Values ('Emp_01', 'Alemu Kebede', 'BR_01', '', 'Jun 15, 2020', '3', '0911223344'),
('Emp_02', 'Debebe Fikadu', 'BR_01', 'Emp_01', 'Jun 15, 2021', '2', '0922334455');

Select * from Employee

Account Type Table


use ABC_Bank_DBMS;

Insert Into Account_Type


Values ('Saving', 'Interest_Rate'), ('Checking', 'Overdrafts');

Select * from Account_Type

Account Table
use ABC_Bank_DBMS;

Insert Into Account


Values ('ACC_01', '150000', 'May 6, 2022', 'Saving', 'CU_01'), ('ACC_02', '2000000', 'Feb
10, 2022', 'Checking', 'CU_02');

Select * from Account

Loan Table
use ABC_Bank_DBMS;

Insert Into Loan


Values ('LO_01', '1000000', 'CU_01');

Select * from Loan

Payment Table
use ABC_Bank_DBMS;

Insert Into Payment


Values ('Pay_01', '10000', 'Sep 10, 2022', 'LO_01'), ('Pay_02', '12000', 'Oct 10, 2022',
'LO_01'), ('Pay_03', '8000', 'Nov 10, 2022', 'LO_01'), ('Pay_04', '9000', 'Dec 10, 2022',
'LO_01'), ('Pay_05', '11000', 'Jan 10, 2023', 'LO_01');

Select * from Payment

4.1. Views
CREATE VIEW [Bank Customers Address] AS
SELECT Customer_Name, Street_City
FROM Customer
WHERE Personal_Banker = 'Emp_02';

CREATE VIEW [Bank Customers Assistants] AS


SELECT Customer_Name, Personal_Banker
FROM Customer

CREATE VIEW [All Employees] AS


SELECT Emp_Name
FROM Employee

CREATE VIEW [Employement Start Date] AS


SELECT Emp_Name, Emp_Date
FROM Employee

CREATE VIEW [Loan Amount] AS


SELECT Loan_Amount, CU_ID
FROM Loan

CREATE VIEW [Payments] AS


SELECT Payment_Number, Payment_Amount, Payment_Date
FROM Payment
WHERE Loan_ID = 'LO_01';

CREATE VIEW [List All Branches] AS


SELECT Branch_Name
FROM Branch

CREATE VIEW [Branch 01 Assets] AS


SELECT Asset_Name
FROM Asset
WHERE Branch_Name = 'BR_01';

CREATE VIEW [Branch 02 Assets] AS


SELECT Asset_Name
FROM Asset
WHERE Branch_Name = 'BR_02';

CREATE VIEW [Saving Accounts] AS


SELECT Account_Number
FROM Account
WHERE Account_Type = 'Saving';

CREATE VIEW [Checking Accounts] AS


SELECT Account_Number
FROM Account
WHERE Account_Type = 'Checking';

CREATE VIEW [Customer 01 Account] AS


SELECT Account_Number
FROM Account
WHERE CU_ID = 'CU_01';

4.2. Procedures
CREATE PROCEDURE SelectAllEmployees
AS
SELECT * FROM Employee
GO;
CREATE PROCEDURE Customers_Account
AS
SELECT * FROM Account WHERE CU_ID = 'ACC_01'

CREATE PROCEDURE First_Account @ACC_01 nvarchar(30)


AS
SELECT * FROM Account WHERE Account_Number = @ACC_01

CREATE PROCEDURE Branch_01_Asset @BR_01 nvarchar(30)


AS
SELECT * FROM Asset WHERE Branch_Name = @BR_01

CREATE PROCEDURE Customers_Loan @CU_01 nvarchar(30)


AS
SELECT * FROM Loan WHERE CU_ID = @CU_01

5. Triggers
CREATE TABLE Account_Audit (
Change_ID INT IDENTITY PRIMARY KEY,
Account_Number varchar(255) not null,
Balance int,
Date_Accessed date,
Account_Type varchar(255),
CU_ID varchar(255),
updated_at DATETIME NOT NULL,
operation CHAR(3) NOT NULL,
CHECK(operation = 'INS' or operation='DEL')
);

CREATE TRIGGER Account_Audit_Trigger


ON Account
After INSERT, DELETE
AS
BEGIN
SET Nocount ON;
INSERT INTO Account_Audit
(
Account_Number,
Balance,
Date_Accessed,
Account_Type,
CU_ID,
updated_at,
operation
)
Select
i.Account_Number,
i.Balance,
Date_Accessed,
Account_Type,
CU_ID,
GETDATE(),
'INS'
FROM
inserted i
UNION ALL
SELECT
d.Account_Number,
d.Balance,
Date_Accessed,
Account_Type,
CU_ID,
GETDATE(),
'DEL'
FROM
deleted d
END

Test the triggers


Insert a new value to the account table
Insert into Account(
Account_Number,
Balance,
Date_Accessed,
Account_Type,
CU_ID
)
Values(
'ACC_03',
'50000',
'Jan 1, 2023',
'Saving',
'CU_02'
);

Check the table Account_Audit


select * from Account_Audit

You might also like