No history yet

Relational Databases

The Orderly World of Tables

A relational database organizes information into tables, much like a collection of neatly arranged spreadsheets. Each table stores data about a specific type of thing, like customers or products. A table is made up of columns (attributes) and rows (records). For instance, a Customers table would have columns for CustomerID, Name, and Email. Each row would represent a single customer.

The real power comes from the “relational” part. These tables don't live in isolation; they can be linked together. Imagine you also have an Orders table. Instead of re-typing a customer's full name and email for every order they place, you can simply use their unique CustomerID to link an order back to the Customers table. This creates a relationship between the two tables, keeping your data organized and efficient.

Keeping Data Clean and Tidy

Storing data is one thing; keeping it reliable is another. Relational databases excel at maintaining data integrity, which means ensuring the data is accurate, consistent, and trustworthy.

One of the key techniques for achieving this is called normalization. This is the process of organizing the columns and tables in a database to minimize data redundancy. Why does that matter? Redundant data wastes space and, more importantly, creates potential for errors. If a customer's email is stored in ten different places, changing it means you have to update it in all ten spots. Miss one, and your data is now inconsistent.

Normalization helps eliminate these update, insertion, and deletion anomalies by ensuring data is stored in only one place.

The process involves following a set of rules called normal forms. While there are several, most databases are designed to meet the Third Normal Form (3NF).

  • 1NF (First Normal Form): Ensures that table cells hold single values and that there are no repeating groups of columns.
  • 2NF (Second Normal Form): Builds on 1NF and requires that all non-key columns are fully dependent on the primary key.
  • 3NF (Third Normal Form): Builds on 2NF and removes transitive dependencies, meaning non-key columns shouldn't depend on other non-key columns.
Lesson image

Speaking the Language of Data

To interact with a relational database, you need to speak its language. That language is SQL, or Structured Query Language. It’s the standard for managing and manipulating data in relational databases.

SQL commands can be grouped by their function:

CategoryPurposeCommon Commands
DQL (Data Query)Retrieve dataSELECT
DML (Data Manipulation)Add, change, or remove dataINSERT, UPDATE, DELETE
DDL (Data Definition)Define or alter the database structureCREATE TABLE, ALTER TABLE
DCL (Data Control)Manage permissions and accessGRANT, REVOKE

Using these commands, you can ask complex questions. For example, you could retrieve the names of all customers who bought a specific product by joining the Customers and Orders tables.

SELECT Customers.Name
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.Product = 'Laptop';

This query looks at both tables, finds matching records based on CustomerID, and returns only the names of customers who ordered a laptop. This ability to perform complex queries across related tables is a core strength of SQL.

The ACID Test

Relational databases are known for being reliable, especially for transactions like financial payments or inventory management. This reliability is guaranteed by a set of four properties known as ACID.

ACID

noun

A set of properties of database transactions intended to guarantee data validity despite errors, power failures, and other mishaps.

Atomicity: Transactions are all-or-nothing. If you transfer $100 from a savings to a checking account, two things must happen: savings is debited, and checking is credited. If the power goes out after the debit but before the credit, the entire transaction is rolled back. The database is left in its original state as if nothing ever happened.

Consistency: A transaction can only bring the database from one valid state to another. It preserves all the predefined rules, like account balances not being allowed to drop below zero. The database is always in a consistent state before and after the transaction.

Isolation: Multiple transactions occurring at the same time do not interfere with each other. If two people try to book the last seat on a flight simultaneously, isolation ensures that only one transaction succeeds. The other will fail as if it happened after the first one was complete.

Durability: Once a transaction has been committed, it will remain so, even in the event of a power loss, crash, or error. When you get a confirmation that your money has been transferred, you can be sure that change is permanent and has been saved to disk.

Strengths and Weaknesses

Relational databases are a mature, powerful technology, but they aren't the perfect solution for every problem.

Advantages:

  • Data Integrity: The strict schema and ACID properties provide a high degree of reliability and consistency.
  • Ease of Use: The tabular structure is intuitive, and SQL is a powerful, standardized language that many developers and analysts know.
  • Flexibility: You can perform complex queries to join, filter, and aggregate data from many tables in sophisticated ways.

Limitations:

  • Rigid Schema: You must define your table structure upfront. Changing the schema later can be complex and slow, making it less suitable for data that is unstructured or rapidly evolving.
  • Scalability: Scaling a relational database to handle massive traffic often involves buying a bigger, more powerful server (vertical scaling), which can be expensive. Scaling out across many smaller servers (horizontal scaling) is traditionally more difficult for relational systems.

Choosing the right database depends on the specific needs of an application. For systems where data consistency and complex queries are paramount, like banking or e-commerce, relational databases remain an excellent choice.

Quiz Questions 1/5

What is the primary purpose of normalization in a relational database?

Quiz Questions 2/5

A banking transaction involves transferring money from a savings account to a checking account. If the system crashes after debiting the savings account but before crediting the checking account, the entire transaction is rolled back. Which ACID property guarantees this outcome?