No history yet

Introduction to Relational Databases

Organizing Information

Imagine trying to run a business using a messy pile of notebooks. Customer information is in one, sales are in another, and product lists are on sticky notes. Finding a connection, like which customer bought which product, would be a nightmare.

A relational database solves this problem. It's a system for storing information in a highly organized way, making it easy to access and manage. Think of it not as a pile of notebooks, but as a digital filing cabinet where all the files are neatly organized in folders (tables) and cross-referenced.

A relational database is a type of database that stores and organizes data points with defined relationships for easy access.

This structure is the foundation for most of the data we interact with daily, from online shopping carts to banking apps. The software that manages these databases is called a Relational Database Management System, or RDBMS.

The Building Blocks

Relational databases have three main components: tables, columns, and rows. Let's break down each one using the example of a simple online store.

Tables: A table stores information about a single subject. For instance, you would have one table for your Customers and a separate table for your Products. You wouldn't mix customer and product information in the same table.

Columns (or Attributes): A column represents a specific piece of information about the table's subject. In a Customers table, you might have columns for CustomerID, FirstName, LastName, and Email. Each column holds a particular type of data.

Rows (or Records): A row contains the actual information for a single entry in the table. Each row in the Customers table would represent one specific customer, with their unique ID, first name, last name, and email.

Here's what a simple Customers table might look like:

CustomerIDFirstNameLastNameEmail
1MariaGarciam.garcia@email.com
2ChenWeichen.wei@email.com
3SamJonessamjones@email.com

Making the Connection

Having organized tables is great, but the real power comes from the "relational" part—the ability to connect them. If we have a Customers table and an Orders table, how do we know which customer placed which order? We do this using keys.

Primary Key

noun

A column (or set of columns) that uniquely identifies each row in a table. No two rows in the same table can have the same primary key.

A primary key is like a student ID number or a social security number. It's a guarantee that each entry is unique and can be found without ambiguity.

Now, to link tables, we use a foreign key.

Foreign Key

noun

A column in one table that refers to the primary key in another table. It acts as a bridge, creating a relationship between the two tables.

Let's see this in action. We have our Customers table, and here is a corresponding Orders table.

OrderIDCustomerIDOrderDateAmount
10132023-10-26$45.50
10212023-10-26$19.99
10332023-10-27$105.00

The CustomerID column in the Orders table is the foreign key. It points back to the primary key in the Customers table. This simple link allows us to see that Sam Jones (CustomerID 3) placed two orders.

The Advantages

So why go to all this trouble? This relational structure provides several key benefits.

It reduces repetition. We only need to store Sam Jones's name and email once, in the Customers table. We can link hundreds of his orders back to that single record without ever typing his name again. This saves space and prevents typos.

It protects data integrity. The database can enforce rules, like preventing an order from being created for a CustomerID that doesn't exist. This ensures the data stays accurate and consistent.

It's flexible. Because the data is broken down into clean, related tables, we can combine it in countless ways to answer complex questions. We could easily find all products bought by customers from a certain city, or the total sales for a specific month.

Understanding this structure of tables, keys, and relationships is the first step to mastering how to work with data. It provides the blueprint for how information is stored and connected.

Quiz Questions 1/6

What is the primary purpose of a relational database?

Quiz Questions 2/6

In a relational database, what is a column?