Banking System Database Implementation in SQL Serv
Banking System Database Implementation in SQL Serv
Server
I'll help you build a comprehensive banking system database using SQL Server. This
implementation follows industry best practices and includes all the components specified in your
project requirements.
Project Overview
This banking system database is designed to manage customer information, accounts,
transactions, and banking operations through a robust relational database structure. The system
implements proper data integrity, security measures, and performance optimizations suitable for
a banking environment [1] [2] [3] .
Database Architecture
Core Entities
The banking system consists of six main entities that work together to provide comprehensive
banking functionality [2] [4] :
1. Customers - Store customer personal information
2. AccountTypes - Define different types of accounts (Savings, Checking, Business, Premium)
3. Accounts - Manage individual customer accounts
4. TransactionTypes - Define transaction categories
5. Transactions - Record all financial transactions
6. AuditLog - Track all database changes for security and compliance
-- =====================================================
-- Banking System Database - Table Definitions
-- SQL Server Implementation
-- =====================================================
USE BankingSystem;
GO
-- =====================================================
-- 1. CUSTOMERS TABLE
-- =====================================================
CREATE TABLE Customers (
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
DateOfBirth DATE NOT NULL,
Email NVARCHAR(100) UNIQUE NOT NULL,
PhoneNumber NVARCHAR(15) NOT NULL,
Address NVARCHAR(200) NOT NULL,
City NVARCHAR(50) NOT NULL,
State NVARCHAR(50) NOT NULL,
ZipCode NVARCHAR(10) NOT NULL,
SSN NVARCHAR(11) UNIQUE NOT NULL,
CreatedDate DATETIME2 DEFAULT GETDATE(),
LastModified DATETIME2 DEFAULT GETDATE(),
IsActive BIT DEFAULT 1
);
-- =====================================================
-- 2. ACCOUNT TYPES TABLE
-- =====================================================
CREATE TABLE AccountTypes (
AccountTypeID INT IDENTITY(1,1) PRIMARY KEY,
TypeName NVARCHAR(50) NOT NULL UNIQUE,
Description NVARCHAR(200),
MinimumBalance DECIMAL(15,2) DEFAULT 0.00,
InterestRate DECIMAL(5,4) DEFAULT 0.0000,
IsActive BIT DEFAULT 1
);
-- =====================================================
-- 3. ACCOUNTS TABLE
-- =====================================================
CREATE TABLE Accounts (
AccountID INT IDENTITY(1,1) PRIMARY KEY,
AccountNumber NVARCHAR(20) UNIQUE NOT NULL,
CustomerID INT NOT NULL,
AccountTypeID INT NOT NULL,
Balance DECIMAL(15,2) DEFAULT 0.00,
OpenDate DATETIME2 DEFAULT GETDATE(),
LastTransactionDate DATETIME2,
Status NVARCHAR(20) DEFAULT 'Active' CHECK (Status IN ('Active', 'Inactive', 'Frozen'
CreatedDate DATETIME2 DEFAULT GETDATE(),
LastModified DATETIME2 DEFAULT GETDATE(),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (AccountTypeID) REFERENCES AccountTypes(AccountTypeID)
);
-- =====================================================
-- 4. TRANSACTION TYPES TABLE
-- =====================================================
CREATE TABLE TransactionTypes (
TransactionTypeID INT IDENTITY(1,1) PRIMARY KEY,
TypeName NVARCHAR(50) NOT NULL UNIQUE,
Description NVARCHAR(200),
IsDebit BIT NOT NULL,
IsActive BIT DEFAULT 1
);
-- =====================================================
-- 5. TRANSACTIONS TABLE
-- =====================================================
CREATE TABLE Transactions (
TransactionID INT IDENTITY(1,1) PRIMARY KEY,
TransactionNumber NVARCHAR(50) UNIQUE NOT NULL,
FromAccountID INT,
ToAccountID INT,
TransactionTypeID INT NOT NULL,
Amount DECIMAL(15,2) NOT NULL CHECK (Amount > 0),
Description NVARCHAR(500),
TransactionDate DATETIME2 DEFAULT GETDATE(),
Status NVARCHAR(20) DEFAULT 'Completed' CHECK (Status IN ('Pending', 'Completed', 'Fa
ProcessedBy NVARCHAR(50),
CreatedDate DATETIME2 DEFAULT GETDATE(),
FOREIGN KEY (FromAccountID) REFERENCES Accounts(AccountID),
FOREIGN KEY (ToAccountID) REFERENCES Accounts(AccountID),
FOREIGN KEY (TransactionTypeID) REFERENCES TransactionTypes(TransactionTypeID)
);
-- =====================================================
-- 6. AUDIT LOG TABLE
-- =====================================================
CREATE TABLE AuditLog (
AuditID INT IDENTITY(1,1) PRIMARY KEY,
TableName NVARCHAR(50) NOT NULL,
OperationType NVARCHAR(10) NOT NULL CHECK (OperationType IN ('INSERT', 'UPDATE', 'DEL
RecordID INT NOT NULL,
OldValues NVARCHAR(MAX),
NewValues NVARCHAR(MAX),
ModifiedBy NVARCHAR(50),
ModifiedDate DATETIME2 DEFAULT GETDATE()
);
-- =====================================================
-- INSERT DEFAULT DATA
-- =====================================================
-- =====================================================
-- CREATE INDEXES FOR PERFORMANCE
-- =====================================================
CREATE NONCLUSTERED INDEX IX_Customers_Email ON Customers(Email);
CREATE NONCLUSTERED INDEX IX_Customers_SSN ON Customers(SSN);
CREATE NONCLUSTERED INDEX IX_Accounts_AccountNumber ON Accounts(AccountNumber);
CREATE NONCLUSTERED INDEX IX_Accounts_CustomerID ON Accounts(CustomerID);
CREATE NONCLUSTERED INDEX IX_Transactions_FromAccountID ON Transactions(FromAccountID);
CREATE NONCLUSTERED INDEX IX_Transactions_ToAccountID ON Transactions(ToAccountID);
CREATE NONCLUSTERED INDEX IX_Transactions_TransactionDate ON Transactions(TransactionDate
CREATE NONCLUSTERED INDEX IX_Transactions_TransactionNumber ON Transactions(TransactionNu
-- =====================================================
-- Banking System Database - Stored Procedures
-- SQL Server Implementation
-- =====================================================
USE BankingSystem;
GO
-- =====================================================
-- CREATE SEQUENCES FOR UNIQUE NUMBERS
-- =====================================================
CREATE SEQUENCE seq_AccountNumber AS INT START WITH 1000000001;
GO
-- =====================================================
-- 1. CREATE CUSTOMER PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_CreateCustomer
@FirstName NVARCHAR(50),
@LastName NVARCHAR(50),
@DateOfBirth DATE,
@Email NVARCHAR(100),
@PhoneNumber NVARCHAR(15),
@Address NVARCHAR(200),
@City NVARCHAR(50),
@State NVARCHAR(50),
@ZipCode NVARCHAR(10),
@SSN NVARCHAR(11),
@CustomerID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
GO
-- =====================================================
-- 2. OPEN ACCOUNT PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_OpenAccount
@CustomerID INT,
@AccountTypeID INT,
@InitialDeposit DECIMAL(15,2) = 0.00,
@AccountID INT OUTPUT,
@AccountNumber NVARCHAR(20) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
GO
-- =====================================================
-- 3. DEPOSIT MONEY PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_DepositMoney
@AccountNumber NVARCHAR(20),
@Amount DECIMAL(15,2),
@Description NVARCHAR(500) = 'Cash deposit'
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Validate amount
IF @Amount <= 0
BEGIN
RAISERROR('Deposit amount must be greater than zero.', 16, 1);
RETURN;
END
IF @AccountID IS NULL
BEGIN
RAISERROR('Account not found or inactive.', 16, 1);
RETURN;
END
-- Record transaction
DECLARE @TransactionNumber NVARCHAR(50) = 'TXN' + FORMAT(NEXT VALUE FOR seq_Trans
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
GO
-- =====================================================
-- 4. WITHDRAW MONEY PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_WithdrawMoney
@AccountNumber NVARCHAR(20),
@Amount DECIMAL(15,2),
@Description NVARCHAR(500) = 'Cash withdrawal'
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Validate amount
IF @Amount <= 0
BEGIN
RAISERROR('Withdrawal amount must be greater than zero.', 16, 1);
RETURN;
END
IF @AccountID IS NULL
BEGIN
RAISERROR('Account not found or inactive.', 16, 1);
RETURN;
END
-- Record transaction
DECLARE @TransactionNumber NVARCHAR(50) = 'TXN' + FORMAT(NEXT VALUE FOR seq_Trans
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
GO
-- =====================================================
-- 5. TRANSFER MONEY PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_TransferMoney
@FromAccountNumber NVARCHAR(20),
@ToAccountNumber NVARCHAR(20),
@Amount DECIMAL(15,2),
@Description NVARCHAR(500) = 'Account transfer'
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Validate amount
IF @Amount <= 0
BEGIN
RAISERROR('Transfer amount must be greater than zero.', 16, 1);
RETURN;
END
IF @FromAccountID IS NULL
BEGIN
RAISERROR('Source account not found or inactive.', 16, 1);
RETURN;
END
-- Get destination account information
DECLARE @ToAccountID INT;
IF @ToAccountID IS NULL
BEGIN
RAISERROR('Destination account not found or inactive.', 16, 1);
RETURN;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
GO
-- =====================================================
-- 6. VIEW TRANSACTION HISTORY PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_GetTransactionHistory
@AccountNumber NVARCHAR(20),
@StartDate DATETIME2 = NULL,
@EndDate DATETIME2 = NULL,
@TransactionType NVARCHAR(50) = NULL,
@MaxRecords INT = 100
AS
BEGIN
SET NOCOUNT ON;
IF @EndDate IS NULL
SET @EndDate = GETDATE();
-- Get account ID
DECLARE @AccountID INT;
IF @AccountID IS NULL
BEGIN
RAISERROR('Account not found.', 16, 1);
RETURN;
END
-- =====================================================
-- 7. GET ACCOUNT BALANCE PROCEDURE
-- =====================================================
CREATE OR ALTER PROCEDURE sp_GetAccountBalance
@AccountNumber NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
SELECT
a.AccountNumber,
c.FirstName + ' ' + c.LastName AS CustomerName,
at.TypeName AS AccountType,
a.Balance AS CurrentBalance,
at.MinimumBalance,
a.Status AS AccountStatus,
a.LastTransactionDate
FROM Accounts a
INNER JOIN Customers c ON a.CustomerID = c.CustomerID
INNER JOIN AccountTypes at ON a.AccountTypeID = at.AccountTypeID
WHERE a.AccountNumber = @AccountNumber;
IF @@ROWCOUNT = 0
BEGIN
RAISERROR('Account not found.', 16, 1);
END
END;
GO
Usage Examples
EXEC sp_CreateCustomer
@FirstName = 'John',
@LastName = 'Doe',
@DateOfBirth = '1990-01-15',
@Email = '[email protected]',
@PhoneNumber = '555-123-4567',
@Address = '123 Main Street',
@City = 'New York',
@State = 'NY',
@ZipCode = '10001',
@SSN = '123-45-6789',
@CustomerID = @CustomerID OUTPUT;
SELECT @CustomerID AS NewCustomerID;
Opening an Account
EXEC sp_OpenAccount
@CustomerID = 1,
@AccountTypeID = 1, -- Savings Account
@InitialDeposit = 500.00,
@AccountID = @AccountID OUTPUT,
@AccountNumber = @AccountNumber OUTPUT;
-- Deposit money
EXEC sp_DepositMoney
@AccountNumber = 'ACC1000000001',
@Amount = 1000.00,
@Description = 'Salary deposit';
-- Withdraw money
EXEC sp_WithdrawMoney
@AccountNumber = 'ACC1000000001',
@Amount = 200.00,
@Description = 'ATM withdrawal';
EXEC sp_GetTransactionHistory
@AccountNumber = 'ACC1000000001',
@StartDate = '2024-01-01',
@EndDate = '2024-12-31',
@MaxRecords = 50;
Security and Best Practices Implementation
This banking system implementation incorporates several critical security and performance
features [8] [9] [10] :
Performance Optimizations
Indexing Strategy: Strategic indexes on frequently queried columns [9]
Sequence Objects: Efficient generation of unique account and transaction numbers
Parameterized Queries: Protection against SQL injection attacks [8] [14]
1. https://github.com/ankittkp/Bank-Database-Design
2. https://www.geeksforgeeks.org/sql/how-to-design-a-database-for-online-banking-system/
3. https://vertabelo.com/blog/database-design-for-banking-system/
4. https://www.scaler.com/topics/er-diagram-for-bank-database/
5. Development_Of_Banking_System_Database.pdf
6. https://www.w3schools.com/sql/sql_stored_procedures.asp
7. https://www.datacamp.com/tutorial/sql-stored-procedure
8. https://en.wikibooks.org/wiki/Microsoft_SQL_Server/Best_Practices
9. https://blog.sqlauthority.com/2010/07/30/sqlauthority-news-authors-birthday-5-sql-server-best-practi
ces/
10. https://www.sqlshack.com/sql-server-transaction-overview/
11. https://www.ibm.com/think/topics/relational-databases
12. https://www.acceldata.io/blog/what-is-a-relational-database-architecture-features-and-real-world-ap
plications
13. https://www.tsql.info/transactions.php
14. https://www.sqlservercentral.com/forums/topic/sql-best-practices-universal-checklist
15. https://satoricyber.com/sql-server-security/sql-server-security-best-practices/
16. https://github.com/sajid0019/Bank-Management-System