What is a Relational Database?

A foundational guide to relational databases. Learn the core concepts of tables, rows, columns, and keys, and understand how SQL is used to define and manipulate the structured data within them.

At the heart of most applications lies a database, a structured system for storing and retrieving data. The most common and enduring type of database is the relational database.

Relational databases have been the workhorse of the software industry for decades, and they are the foundation for everything from banking systems to e-commerce websites. Popular relational databases include MySQL, PostgreSQL, SQL Server, and Oracle.

The Core Idea: Storing Data in Tables

The core idea of a relational database is simple: data is organized into tables. You can think of a table just like a spreadsheet.

  • A table represents a type of entity, like Customers or Products.
  • A row (or record) in the table represents a single instance of that entity, like a specific customer or product.
  • A column (or field) represents an attribute of that entity, like FirstName or Price.

For example, a Customers table might look like this:

CustomerID FirstName LastName Email
1 Alice Smith alice@example.com
2 Bob Johnson bob.j@example.com

The Schema: Defining the Structure

Unlike some other types of databases, a relational database has a predefined structure, which is called a schema. The schema defines the tables, the columns in each table, and the data type for each column (e.g., INTEGER, VARCHAR for strings, DATETIME).

This strict schema ensures data integrity, meaning the data in your database is reliable and consistent.

Keys: Creating Relationships

The "relational" part of the name comes from the ability to define relationships between tables. This is done using keys.

  • Primary Key: A primary key is a column (or a set of columns) that uniquely identifies each row in a table. In our Customers table, CustomerID is the primary key. No two customers can have the same CustomerID.

  • Foreign Key: A foreign key is a column in one table that refers to the primary key of another table. This is how you create a link between tables.

Let's say we have an Orders table:

OrderID OrderDate CustomerID (Foreign Key)
101 2020-02-24 1
102 2020-02-24 2
103 2020-02-25 1

The CustomerID column in the Orders table is a foreign key that points to the CustomerID primary key in the Customers table. This tells us that orders 101 and 103 belong to Alice, and order 102 belongs to Bob.

SQL: The Language of Relational Databases

To interact with a relational database, you use SQL (Structured Query Language). SQL is the standard language for querying, manipulating, and defining the data.

With SQL, you can:

  • SELECT: Retrieve data from tables.
  • INSERT: Add new rows to tables.
  • UPDATE: Modify existing rows.
  • DELETE: Remove rows.
  • JOIN: Combine rows from multiple tables based on their relationships.

For example, to get a list of all orders placed by Alice Smith, you would write a JOIN query:

SELECT Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Customers.FirstName = 'Alice' AND Customers.LastName = 'Smith';

ACID Properties

Relational databases are known for being ACID compliant, which is a set of properties that guarantee the reliability of database transactions.

  • Atomicity: All the operations in a transaction either all succeed or all fail together.
  • Consistency: The database remains in a consistent state before and after the transaction.
  • Isolation: Concurrent transactions do not interfere with each other.
  • Durability: Once a transaction is committed, it will remain committed, even in the event of a system failure.

Conclusion

Relational databases provide a powerful, reliable, and time-tested way to store and manage structured data. By organizing data into tables and using the power of SQL to define and query relationships between them, they provide a solid foundation for a vast range of applications. While other database models like NoSQL have emerged for specific use cases, the relational model remains a cornerstone of the software world.