Bank management system database: ER diagram and SQL

A bank management system database in 8 tables: branches, customers, account types, accounts, transactions, cards, loans and loan payments. The ER diagram, what each table holds, and the SQL Server script, ready for a DBMS project or a real app and open to change in ERDsmith.

  • SQL Server
  • 8 tables
  • 8 relationships
ER diagram of the bank database: dbo.Customer, dbo.Branch and dbo.AccountType with dbo.Account, dbo.AccountTransaction, dbo.Card, dbo.Loan and dbo.LoanPayment, with their keys and relationships.
Drawn by ERDsmith from the script below. Open it to move tables, add columns or pick the IDEF1X look preset.

The tables

Eight tables in four groups: the bank’s branches and products, the customers, the money on their accounts, and the loans.

  • dbo.BranchBank branches, found by branch code, with their city.
  • dbo.AccountTypeChecking, savings and the like, each name once, with its interest rate.
  • dbo.CustomerOne row per customer, found by customer number.
  • dbo.AccountAn account held by one customer at one branch: opened and closed dates.
  • dbo.AccountTransactionOne deposit or withdrawal: a positive or negative amount and when it was posted.
  • dbo.CardA debit or credit card on an account, its expiry, and the day it was blocked.
  • dbo.LoanA loan to a customer from a branch: principal, rate, term in months.
  • dbo.LoanPaymentOne installment paid on a loan; its key is the loan and the installment number.

How the tables relate

  • A customer has zero or more accounts , and each account belongs to exactly one customer: CustomerId is NOT NULL, so the line has nothing at the customer’s end .
  • An account is opened at one branch and is of one account type; a loan is given by one branch too.
  • An account has zero or more transactions and zero or more cards.
  • A loan has zero or more payments. dbo.LoanPayment is keyed by (LoanId, PaymentNumber), a composite key, so its line from dbo.Loan is solid and its box has round corners .
  • Customers and branches meet only through accounts and loans: a customer can bank at any number of branches.

Why it is built this way

The balance is the sum of the transactions

There is no balance column to fall out of step. A transaction is a signed amount, a deposit above zero and a withdrawal below, and the check CK_AccountTransaction_Amount refuses a zero amount with Msg 547. The balance of every open account:

SELECT a.AccountNumber, c.FullName,
    COALESCE(SUM(t.Amount), 0) AS Balance
FROM dbo.Account AS a
JOIN dbo.Customer AS c ON c.CustomerId = a.CustomerId
LEFT JOIN dbo.AccountTransaction AS t
    ON t.AccountId = a.AccountId
WHERE a.ClosedOn IS NULL
GROUP BY a.AccountNumber, c.FullName;

Nothing in the schema stops a withdrawal that takes the balance below zero. The application reads the balance and inserts the withdrawal in one transaction.

One active card of each type per account

A filtered unique index on dbo.Card (AccountId, CardType) WHERE BlockedOn IS NULL allows one active debit card and one active credit card per account. A second active debit card fails with Msg 2601. Blocking the old card sets BlockedOn, and then the replacement goes in; the blocked card stays as history.

An installment is paid once

The primary key (LoanId, PaymentNumber) on dbo.LoanPayment refuses installment 1 of the same loan twice with Msg 2627, so a retry or a double click at the counter can’t post the same installment again.

Customers have an id and a customer number

CustomerId is a surrogate key the accounts and loans carry; the customer number is an alternate key, the number printed on the passbook. An account for a customer that doesn’t exist fails with Msg 547, the foreign key’s referential integrity check.

The SQL Server script

bank.sql · 135 linesDownload
-- Table [dbo].[AccountType]
CREATE TABLE [dbo].[AccountType] (
    [AccountTypeId] INT            IDENTITY (1, 1) NOT NULL,
    [Name]          NVARCHAR (50)  NOT NULL,
    [InterestRate]  DECIMAL (5, 2) NOT NULL,
    CONSTRAINT [PK_AccountType] PRIMARY KEY CLUSTERED ([AccountTypeId] ASC),
    CONSTRAINT [CK_AccountType_InterestRate] CHECK (InterestRate >= 0),
    CONSTRAINT [AK_AccountType_Name] UNIQUE NONCLUSTERED ([Name] ASC)
);


GO

-- Table [dbo].[Branch]
CREATE TABLE [dbo].[Branch] (
    [BranchId]   INT            IDENTITY (1, 1) NOT NULL,
    [BranchCode] VARCHAR (10)   NOT NULL,
    [Name]       NVARCHAR (100) NOT NULL,
    [City]       NVARCHAR (100) NOT NULL,
    CONSTRAINT [PK_Branch] PRIMARY KEY CLUSTERED ([BranchId] ASC),
    CONSTRAINT [AK_Branch_BranchCode] UNIQUE NONCLUSTERED ([BranchCode] ASC)
);


GO

-- Table [dbo].[Customer]
CREATE TABLE [dbo].[Customer] (
    [CustomerId]     INT            IDENTITY (1, 1) NOT NULL,
    [CustomerNumber] VARCHAR (20)   NOT NULL,
    [FullName]       NVARCHAR (200) NOT NULL,
    [BirthDate]      DATE           NOT NULL,
    [Email]          NVARCHAR (254) NULL,
    [Phone]          VARCHAR (20)   NULL,
    CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ([CustomerId] ASC),
    CONSTRAINT [AK_Customer_CustomerNumber] UNIQUE NONCLUSTERED ([CustomerNumber] ASC)
);


GO

-- Table [dbo].[Account]
CREATE TABLE [dbo].[Account] (
    [AccountId]     INT          IDENTITY (1, 1) NOT NULL,
    [AccountNumber] VARCHAR (20) NOT NULL,
    [CustomerId]    INT          NOT NULL,
    [BranchId]      INT          NOT NULL,
    [AccountTypeId] INT          NOT NULL,
    [OpenedOn]      DATE         NOT NULL,
    [ClosedOn]      DATE         NULL,
    CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED ([AccountId] ASC),
    CONSTRAINT [CK_Account_ClosedOn] CHECK (ClosedOn >= OpenedOn),
    CONSTRAINT [FK_Account_AccountType_AccountTypeId] FOREIGN KEY ([AccountTypeId]) REFERENCES [dbo].[AccountType] ([AccountTypeId]),
    CONSTRAINT [FK_Account_Branch_BranchId] FOREIGN KEY ([BranchId]) REFERENCES [dbo].[Branch] ([BranchId]),
    CONSTRAINT [FK_Account_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]),
    CONSTRAINT [AK_Account_AccountNumber] UNIQUE NONCLUSTERED ([AccountNumber] ASC)
);


GO

-- Table [dbo].[Loan]
CREATE TABLE [dbo].[Loan] (
    [LoanId]       INT             IDENTITY (1, 1) NOT NULL,
    [LoanNumber]   VARCHAR (20)    NOT NULL,
    [CustomerId]   INT             NOT NULL,
    [BranchId]     INT             NOT NULL,
    [Principal]    DECIMAL (12, 2) NOT NULL,
    [InterestRate] DECIMAL (5, 2)  NOT NULL,
    [TermMonths]   SMALLINT        NOT NULL,
    [StartsOn]     DATE            NOT NULL,
    CONSTRAINT [PK_Loan] PRIMARY KEY CLUSTERED ([LoanId] ASC),
    CONSTRAINT [CK_Loan_InterestRate] CHECK (InterestRate >= 0),
    CONSTRAINT [CK_Loan_Principal] CHECK (Principal > 0),
    CONSTRAINT [CK_Loan_TermMonths] CHECK (TermMonths > 0),
    CONSTRAINT [FK_Loan_Branch_BranchId] FOREIGN KEY ([BranchId]) REFERENCES [dbo].[Branch] ([BranchId]),
    CONSTRAINT [FK_Loan_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([CustomerId]),
    CONSTRAINT [AK_Loan_LoanNumber] UNIQUE NONCLUSTERED ([LoanNumber] ASC)
);


GO

-- Table [dbo].[AccountTransaction]
CREATE TABLE [dbo].[AccountTransaction] (
    [AccountTransactionId] INT             IDENTITY (1, 1) NOT NULL,
    [AccountId]            INT             NOT NULL,
    [PostedAt]             DATETIME2 (0)   NOT NULL,
    [Amount]               DECIMAL (12, 2) NOT NULL,
    [Description]          NVARCHAR (200)  NULL,
    CONSTRAINT [PK_AccountTransaction] PRIMARY KEY CLUSTERED ([AccountTransactionId] ASC),
    CONSTRAINT [CK_AccountTransaction_Amount] CHECK (Amount <> 0),
    CONSTRAINT [FK_AccountTransaction_Account_AccountId] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account] ([AccountId])
);


GO

-- Table [dbo].[Card]
CREATE TABLE [dbo].[Card] (
    [CardId]     INT          IDENTITY (1, 1) NOT NULL,
    [CardNumber] CHAR (16)    NOT NULL,
    [AccountId]  INT          NOT NULL,
    [CardType]   VARCHAR (10) NOT NULL,
    [ExpiresOn]  DATE         NOT NULL,
    [BlockedOn]  DATE         NULL,
    CONSTRAINT [PK_Card] PRIMARY KEY CLUSTERED ([CardId] ASC),
    CONSTRAINT [CK_Card_CardType] CHECK (CardType IN ('Debit', 'Credit')),
    CONSTRAINT [FK_Card_Account_AccountId] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account] ([AccountId]),
    CONSTRAINT [AK_Card_CardNumber] UNIQUE NONCLUSTERED ([CardNumber] ASC)
);


GO

CREATE UNIQUE NONCLUSTERED INDEX [UX_Card_AccountId_CardType_Active]
    ON [dbo].[Card]([AccountId] ASC, [CardType] ASC) WHERE (BlockedOn IS NULL);


GO

-- Table [dbo].[LoanPayment]
CREATE TABLE [dbo].[LoanPayment] (
    [LoanId]        INT             NOT NULL,
    [PaymentNumber] SMALLINT        NOT NULL,
    [PaidOn]        DATE            NOT NULL,
    [Amount]        DECIMAL (12, 2) NOT NULL,
    CONSTRAINT [PK_LoanPayment] PRIMARY KEY CLUSTERED ([LoanId] ASC, [PaymentNumber] ASC),
    CONSTRAINT [CK_LoanPayment_Amount] CHECK (Amount > 0),
    CONSTRAINT [CK_LoanPayment_PaymentNumber] CHECK (PaymentNumber > 0),
    CONSTRAINT [FK_LoanPayment_Loan_LoanId] FOREIGN KEY ([LoanId]) REFERENCES [dbo].[Loan] ([LoanId])
);


GO

Questions

What tables does a bank management system need?

Customers, accounts and transactions at the least. Branches, account types, cards, loans and loan payments come next. This design has all eight.

How do I get an account's balance?

Sum its rows in dbo.AccountTransaction: deposits are positive, withdrawals negative. The query under "Why it is built this way" does it for every open account.

Can an account have two holders?

Not in this design: dbo.Account has one CustomerId. A joint account needs a table between customers and accounts, one row per holder, keyed by the pair.

Can I change the design?

Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.

Does it work with MySQL or PostgreSQL?

The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types and the filtered index need that database's own syntax.

More templates and reading