MODULO SQL & Database Design Master SeriesLAB

Designing a Comprehensive Banking Database

Introduction

A well-designed banking database is crucial for storing and managing financial data efficiently. In this masterclass, we will delve into the technical aspects of designing a comprehensive banking database. We will cover the key components, relationships, and constraints that make up a robust banking database.

Database Requirements

Before designing the database, let's identify the key requirements:

  • Store customer information
  • Manage accounts (checking, savings, credit cards, loans)
  • Record transactions (deposits, withdrawals, transfers)
  • Track account balances and transaction history
  • Ensure data integrity and security

Entity-Relationship Modeling (ERM)

To design the database, we will use Entity-Relationship Modeling (ERM). ERM is a technique used to identify entities, attributes, and relationships between them.

Entities

  • Customer: represents an individual or business with a unique identifier (Customer ID)
  • Account: represents a specific type of account (checking, savings, credit card, loan)
  • Transaction: represents a single transaction (deposit, withdrawal, transfer)

Attributes

  • Customer:
    • Customer ID (primary key)
    • Name
    • Address
    • Phone Number
    • Email
  • Account:
    • Account ID (primary key)
    • Customer ID (foreign key referencing the Customer table)
    • Account Type (checking, savings, credit card, loan)
    • Balance
  • Transaction:
    • Transaction ID (primary key)
    • Account ID (foreign key referencing the Account table)
    • Transaction Date
    • Transaction Type (deposit, withdrawal, transfer)
    • Amount

Relationships

  • A customer can have multiple accounts (one-to-many).
  • An account is owned by one customer (many-to-one).
  • An account can have multiple transactions (one-to-many).
  • A transaction is related to one account (many-to-one).

Database Schema

Based on the ERM, we can create the following database schema:

CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Address VARCHAR(255) NOT NULL,
    PhoneNumber VARCHAR(20) NOT NULL,
    Email VARCHAR(100) NOT NULL
);

CREATE TABLE Accounts (
    AccountID INT PRIMARY KEY,
    CustomerID INT NOT NULL,
    AccountType VARCHAR(20) NOT NULL,
    Balance DECIMAL(10, 2) NOT NULL,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE Transactions (
    TransactionID INT PRIMARY KEY,
    AccountID INT NOT NULL,
    TransactionDate DATE NOT NULL,
    TransactionType VARCHAR(20) NOT NULL,
    Amount DECIMAL(10, 2) NOT NULL,
    FOREIGN KEY (AccountID) REFERENCES Accounts(AccountID)
);

Constraints

To ensure data integrity, we need to add constraints to the database schema:

  • Primary Key: uniquely identifies each record in a table.
  • Foreign Key: ensures that a record in one table is related to a record in another table.
  • NOT NULL: ensures that a field cannot be left blank.
  • CHECK: ensures that a field value meets a specific condition.
ALTER TABLE Accounts
ADD CONSTRAINT CHK_AccountType CHECK (AccountType IN ('checking', 'savings', 'credit card', 'loan'));

ALTER TABLE Transactions
ADD CONSTRAINT CHK_TransactionType CHECK (TransactionType IN ('deposit', 'withdrawal', 'transfer'));

Indexing

To improve query performance, we can add indexes to the database schema:

  • Clustered Index: reorders the physical records of a table according to the index.
  • Non-Clustered Index: creates a separate structure that contains the index.
CREATE INDEX idx_CustomerID ON Accounts (CustomerID);
CREATE INDEX idx_AccountID ON Transactions (AccountID);

Normalization

To eliminate data redundancy and improve data integrity, we can normalize the database schema:

  • First Normal Form (1NF): each table cell contains a single value.
  • Second Normal Form (2NF): each non-key attribute depends on the entire primary key.
  • Third Normal Form (3NF): if a table is in 2NF, and a non-key attribute depends on another non-key attribute, then it should be moved to a separate table.

The database schema we designed earlier is already normalized to 3NF.

Denormalization

To improve query performance, we can denormalize the database schema:

  • Pre-aggregation: stores pre-calculated values to avoid complex calculations.
  • Summary tables: stores aggregated data to avoid complex queries.

However, denormalization can lead to data redundancy and inconsistencies.

Data Modeling

To visualize the database schema, we can use data modeling techniques:

  • Entity-Relationship Diagrams (ERDs): visualizes the entities, attributes, and relationships between them.
  • Dimensional Modeling: visualizes the database schema as a star or snowflake schema.

SQL Queries

To interact with the database, we can use SQL queries:

  • SELECT: retrieves data from a database table.
  • INSERT: inserts new data into a database table.
  • UPDATE: updates existing data in a database table.
  • DELETE: deletes data from a database table.

Here are some examples of SQL queries:

-- Retrieve all customers
SELECT * FROM Customers;

-- Insert a new customer
INSERT INTO Customers (CustomerID, Name, Address, PhoneNumber, Email)
VALUES (1, 'John Doe', '123 Main St', '123-456-7890', 'john.doe@example.com');

-- Update a customer's address
UPDATE Customers
SET Address = '456 Elm St'
WHERE CustomerID = 1;

-- Delete a customer
DELETE FROM Customers
WHERE CustomerID = 1;

Conclusion

Designing a comprehensive banking database requires careful consideration of the key components, relationships, and constraints. By using Entity-Relationship Modeling, normalization, and indexing, we can create a robust database schema that ensures data integrity and security. Additionally, SQL queries can be used to interact with the database and retrieve, insert, update, and delete data.

⚔️ YOUR MISSION

Design a database schema for a banking system that includes the following entities:

  • Customers
  • Accounts (checking, savings, credit cards, loans)
  • Transactions (deposits, withdrawals, transfers)

Ensure that the database schema is normalized to 3NF and includes constraints to ensure data integrity. Additionally, create a set of SQL queries to interact with the database and retrieve, insert, update, and delete data.

SQL practice editor
Read the exercise prompt
-- Create the database schema
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Address VARCHAR(255) NOT NULL,
    PhoneNumber VARCHAR(20) NOT NULL,
    Email VARCHAR(100) NOT NULL
);

CREATE TABLE Accounts (
    AccountID INT PRIMARY KEY,
    CustomerID INT NOT NULL,
    AccountType VARCHAR(20) NOT NULL,
    Balance DECIMAL(10, 2) NOT NULL,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE Transactions (
    TransactionID INT PRIMARY KEY,
    AccountID INT NOT NULL,
    TransactionDate DATE NOT NULL,
    TransactionType VARCHAR(20) NOT NULL,
    Amount DECIMAL(10, 2) NOT NULL,
    FOREIGN KEY (AccountID) REFERENCES Accounts(AccountID)
);

-- Insert sample data
INSERT INTO Customers (CustomerID, Name, Address, PhoneNumber, Email)
VALUES (1, 'John Doe', '123 Main St', '123-456-7890', 'john.doe@example.com');

INSERT INTO Accounts (AccountID, CustomerID, AccountType, Balance)
VALUES (1, 1, 'checking', 1000.00);

INSERT INTO Transactions (TransactionID, AccountID, TransactionDate, TransactionType, Amount)
VALUES (1, 1, '2022-01-01', 'deposit', 500.00);

-- Retrieve all customers
SELECT * FROM Customers;

-- Retrieve all accounts for a customer
SELECT * FROM Accounts
WHERE CustomerID = 1;

-- Retrieve all transactions for an account
SELECT * FROM Transactions
WHERE AccountID = 1;
WAITING FOR SIGNAL...
BACKNEXT STAGE