Relational Databases#

Relational databases are the workhorse of financial technology systems. Banks, trading platforms, and payment processors rely on them because they offer structured storage, consistency, and reliability at scale.


The Relational Model#

The relational model, introduced by Edgar F. Codd at IBM in 1970, organizes data into tables (relations) composed of rows (tuples/records) and columns (attributes). As you can see below, we will track customers and their accounts separately.

Example: customers

customer_id

first_name

last_name

state

email

5001

Alice

Jones

NC

alice.jones@email.com

5002

Bob

Smith

NY

bob.smith@email.com

5003

Carol

Lee

CA

carol.lee@email.com

5004

David

Patel

TX

david.patel@email.com

In the customers table, we track information about each customer. In a real system, this would be further decomposed into multiple tables to track information such as addresses, phone numbers, and history with the company.

Keys in Relational Databases#

One of the most important ideas in the relational model is the use of keys—attributes (or combinations of attributes) that uniquely identify records and maintain logical consistency between tables. Keys ensure that data is not duplicated, misplaced, or orphaned across a system.

Primary Keys#

A primary key is the unique identifier for each row in a table. It must satisfy two rules:

  1. Uniqueness – no two rows can share the same primary key value.

  2. Non-nullability – every row must have a value for the primary key.

In the customers example, customer_id is the primary key:

customer_id

first_name

last_name

…

5001

Alice

Jones

…

Primary keys provide:

  • Stable identity — even if other attributes change (like a name or email), the key does not.

  • Efficient indexing — databases automatically create indexes on primary keys for fast lookups.

  • Foundation for relationships — other tables reference the primary key through foreign keys.

In financial systems—where correctness is essential—primary keys help ensure that transactions, balances, and customer records always point to the right entities.

Alternate Keys (Candidate Keys)#

A table may have additional attributes that could uniquely identify a row, even if they are not chosen as the primary key. These are called:

  • Candidate keys — all possible unique identifiers.

  • Alternate keys — candidate keys that are not selected as the primary key.

For example, in customers:

  • email should be unique for each customer.

  • Therefore, it is a candidate key.

  • But since customer_id is the primary key, email becomes an alternate key.

Databases often enforce uniqueness on alternate keys using unique constraints. This prevents data errors such as accidentally registering the same person twice with the same email address.

Why Keys Matter in Real Systems#

Keys are essential for:

  • Preventing duplicates (e.g., two accounts with the same account number)

  • Maintaining referential integrity (e.g., every transaction must map to a valid account)

  • Optimizing performance (indexes on key columns make queries faster)

  • Ensuring auditability and traceability — critical in financial applications, where regulatory oversight demands accurate record-keeping.

Together, primary and alternate keys form the backbone of a relational database’s structure, greatly reducing ambiguity and ensuring that data stays accurate as systems grow.

Designing Relations and Understanding Relationships#

Once we establish primary keys for each table, we can begin to design how tables relate to one another. A relational database is not just a collection of independent tables—it is a network of relationships defined through shared keys. Good design ensures that data is stored efficiently, accurately, and without unnecessary duplication.

Types of Relationships#

In most financial systems, you will frequently encounter these relationship patterns:

  • One-to-One (1:1) One record in a table corresponds to at most one record in another table. Example: A customer might have one dedicated tax profile.

  • One-to-Many (1:N) A single record relates to multiple records in another table. This is the most common pattern in banking and payments. Example: A customer can hold several accounts.

  • Many-to-Many (M:N) Records in one table relate to multiple records in another, and vice versa. These require a junction table to model the connections. Example: Joint accounts, where multiple customers share access to multiple accounts.

Understanding these relationships helps determine where keys belong and how tables should be structured.

Why Separate Data Into Multiple Relations?#

Relational design follows principles such as normalization, which encourage splitting data into well-defined tables to:

  • Reduce redundancy — avoid repeating customer info in every account row

  • Improve data quality — update a fact (e.g., a phone number) once, not everywhere

  • Enforce consistency — foreign keys ensure related rows actually exist

  • Scale cleanly — queries operate on smaller, focused tables rather than large, mixed-purpose ones

Instead of one big “everything” table, we build a set of smaller, logical relations that work together through well-defined keys.

Keys Drive Relationships#

With primary keys providing unique identity, relationships are formed using:

  • Foreign keys — columns that point to a primary key in another table

  • Alternate keys — additional unique fields (e.g., email) that may be referenced if appropriate

  • Composite keys — combinations of columns used together to identify a row in certain designs

This structure creates an interconnected system where every table plays a clear role.

As customers can have accounts, we will use a separate table to track those accounts.

Example: Accounts Table

account_id

customer_id

balance

type

101

5001

2,500.00

Checking

102

5002

5,100.00

Savings

103

5001

20,000.00

Brokerage

Just like the customer table, we have a primary key field

  • Table: accounts

  • Row: one account record (e.g., account 101)

  • Column: attribute (e.g., balance)

  • Primary Key: account_id uniquely identifies each row

  • Foreign Key: customer_id references a customer table

This structure avoids redundancy, enforces consistency, and allows powerful queries.

Data Anomalies and Why Design Matters#

If we store all data in one big table, problems emerge:

Example: Single Flat Table

customer_name

account_type

account_id

balance

transaction_date

transaction_amount

Alice Jones

Checking

101

2500.00

2025-01-10

-100.00

Alice Jones

Checking

101

2400.00

2025-01-12

+500.00

Bob Smith

Savings

102

5100.00

NULL

NULL

Problems:

  • Update anomaly: If Alice changes her last name, it must be updated in many rows.

  • Insert anomaly: Cannot add a new customer unless they already have a transaction.

  • Delete anomaly: If Alice’s last transaction is deleted, we might lose record of her account.

Good relational design (splitting into customers, accounts, and transactions) prevents these anomalies.

Normalization#

Normalization is the process of organizing data into tables to reduce redundancy and avoid anomalies.

  • First Normal Form (1NF): Data must be in tabular form, no repeating groups.

  • Second Normal Form (2NF): Every non-key attribute depends on the whole primary key.

  • Third Normal Form (3NF): No transitive dependencies (attributes depend only on the key).

Example: From Unnormalized to Normalized

  • Unnormalized:

    Customer: Alice, Accounts: [Checking-101, Savings-102]
    
  • Normalized (3NF):

    • customers table → (cust_id, name)

    • accounts table → (account_id, cust_id, type, balance)

    • transactions table → (txn_id, account_id, date, amount)

This makes queries easier, updates safer, and storage more efficient.

Entity-Relationship (ER) Modeling#

To design a database, we often start with an ER diagram.

  • Entities: things we want to track (Customers, Accounts, Transactions)

  • Attributes: details of entities (name, balance, amount)

  • Relationships: connections (Customer owns Account, Account has Transaction)

Example: Simple ER Diagram

[Customer] ----(owns)----< [Account] ----(records)----< [Transaction]

This diagram leads directly to relational tables.


SQL: The Language of Relational Databases#

Relational databases use SQL (Structured Query Language).

Data Definition (DDL)#

Create a table:

CREATE TABLE accounts (
    account_id   INT PRIMARY KEY,
    customer_id  INT NOT NULL,
    type         VARCHAR(20) NOT NULL,
    balance      DECIMAL(12,2) NOT NULL,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

Data Manipulation (DML)#

Insert data:

INSERT INTO accounts (account_id, customer_id, type, balance)
VALUES (101, 5001, 'Checking', 2500.00);

Retrieve data:

SELECT c.name, a.type, a.balance
FROM customers c
JOIN accounts a ON c.customer_id = a.customer_id
WHERE a.balance > 5000;

Update data:

UPDATE accounts
SET balance = balance - 100
WHERE account_id = 101;

Delete data:

DELETE FROM accounts
WHERE account_id = 101;

Queries in Action#

  • List all customers in California:

    SELECT * FROM customers WHERE state = 'CA';
    
  • Find the largest transaction:

    SELECT MAX(amount) FROM transactions;
    

Transactions and ACID#

Transactions guarantee consistency and safety.

Example: Transfer $50 from A to B

BEGIN TRANSACTION;

UPDATE accounts
SET balance = balance - 50
WHERE account_id = 101;

UPDATE accounts
SET balance = balance + 50
WHERE account_id = 102;

COMMIT;
  • If the system crashes after the first update but before the second, the DBMS rolls back to keep data consistent.

  • This is what makes relational databases safe for money movement.


Summary#

  • The relational model organizes data into tables with keys and relationships.

  • Normalization avoids redundancy and anomalies.

  • ER diagrams help design databases logically.

  • SQL is the universal language to define, query, and update relational data.

  • Transactions and ACID ensure safety — critical in financial applications.

Next, we’ll explore how to connect to databases with Python, and use them in real-world FinTech applications.