No history yet

Introduction to Relational Databases

Organizing Information

Think of a library. Every book has a specific spot on a shelf, organized by genre, author, and title. This system makes it possible to find any book quickly. A relational database works on a similar principle, but for data. It's a structured way to store and manage information so that it's easy to access and use.

The most common method for organizing this data is the relational model. Instead of shelves and books, it uses a simple and intuitive structure: tables. This model allows us to see not just the data itself, but also the relationships between different pieces of data.

Relational Model: The foundation of an RDBMS is based on the relational model, which organizes data into tables (relations) consisting of rows (tuples) and columns (attributes).

The Building Blocks

At the heart of the relational model are three core components: tables, columns, and rows. Let's break down what each one does using a simple example: a table for storing customer information.

Table: A table is a collection of related data, like a spreadsheet. You'd have one table for customers, another for products, and a third for orders. Each table stores only one type of information.

Column: A column represents a specific attribute or piece of information about the data in the table. For our Customers table, columns might include CustomerID, FirstName, LastName, and Email.

Row: A row is a single record in the table. It contains the actual values for each column, representing one specific customer. Each row tells a complete story about one entry.

Here’s how that Customers table would look:

CustomerIDFirstNameLastNameEmail
101AnyaSharmaanya.s@email.com
102BenCarterben.carter@email.com
103ChloeDavischloe.d@email.com

In this table, Customers is the name of our table. CustomerID, FirstName, LastName, and Email are the columns. Each line of customer data is a row.

Connecting the Dots

The real power of a relational database comes from creating relationships between tables. Storing everything in one massive table would be messy and repetitive. For instance, if we added customer orders to our Customers table, we'd have to repeat Anya Sharma's name and email for every single purchase she makes.

Instead, we can create a separate Orders table. But how do we know which order belongs to which customer? We connect the tables using keys.

Primary Key

noun

A column (or set of columns) that uniquely identifies each row in a table. A primary key value must be unique for each row and cannot be empty (or NULL).

In our Customers table, CustomerID is the perfect primary key. Each customer has a unique ID (101, 102, 103), so we can always identify them precisely.

Foreign Key

noun

A column in one table that is the primary key of another table. It acts as a link or cross-reference between the two tables.

To link an order to a customer, we simply add the CustomerID column to our Orders table. In the Orders table, CustomerID is a foreign key. It “points” back to the primary key in the Customers table, creating a relationship.

Keeping Data Trustworthy

This system of keys does more than just link data. It also helps maintain data integrity, which is a fancy way of saying the data is reliable, accurate, and consistent.

For example, because of the foreign key relationship, the database can enforce a rule: you can't add an order for a CustomerID that doesn't exist in the Customers table. This prevents impossible records, like an order from a ghost customer. It also prevents you from accidentally deleting a customer who has existing orders, which would leave those orders orphaned and meaningless.

By structuring data across related tables, we reduce repetition and protect the logical connections within our information. This makes the data not only more efficient to store but also far more trustworthy.

Now that you understand how relational databases are structured, you're ready to learn how to interact with them.