SQL Fundamentals
Introduction to Relational Databases
Organizing Information
Imagine trying to keep track of customer information for a small online store. You could start with a spreadsheet. You'd have columns for name, email, and shipping address. Each row would represent a single customer. This works well, at first.
But what happens when customers start placing orders? You could add more columns to your spreadsheet for order date, product name, and price. But if a customer places ten orders, you'll have to re-enter their name, email, and address ten times. This is messy and can lead to mistakes. What if a customer changes their address? You’d have to find and update every single entry.
This is the problem that relational databases solve. They provide a structured way to store information in separate, connected buckets, preventing repetition and keeping data clean.
A relational database is a collection of information organized into tables. Think of it as a set of smart spreadsheets that can talk to each other. Instead of one massive, cluttered file, you have several smaller, neater tables, each holding a specific type of information. One table might hold customer data, another might hold product data, and a third could track orders.
The Building Blocks
Every relational database is built from a few simple components: tables, columns, and rows.
Table: A collection of related data. For our store, we might have a
Customerstable. Column: A specific piece of information within a table. In ourCustomerstable, we'd have columns forCustomerID,FirstName,LastName, andCustomerstable would represent one person.
Here's what our Customers table might look like:
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 101 | Maria | Garcia | m.garcia@email.com |
| 102 | Chen | Wei | chen.wei@email.com |
| 103 | Fatima | Khan | f.khan@email.com |
Each piece of information has its own place. The table stores all customer data, each column defines a specific attribute, and each row is a complete record for a single customer.
Making Connections
The real power of relational databases comes from the "relational" part. We can create links, or relationships, between tables. To do this, we need special identifiers called keys.
Primary Key
noun
A column (or set of columns) that uniquely identifies each row in a table. No two rows can have the same primary key, and it cannot be empty.
In our Customers table, CustomerID is the perfect primary key. It's a unique number assigned to each customer. Even if we have two customers named Maria Garcia, their CustomerID will be different.
Now, let's create a separate Orders table. How do we know which customer placed which order? We use a foreign key.
Foreign Key
noun
A column in one table that refers to the primary key of another table. It's the link that creates the relationship between the two tables.
Our Orders table will have its own primary key, OrderID, but it will also include a CustomerID column. This CustomerID column is a foreign key. It contains a value that matches a CustomerID in the Customers table.
This connection is a one-to-many relationship. One customer can have many orders, but each order belongs to only one customer. By splitting the data into two tables and linking them, we've created a more efficient and reliable system. Now, if Maria Garcia updates her email address, we only have to change it in one place: the Customers table.
This is the core idea of relational databases. By organizing data into discrete, related tables, we reduce redundancy, improve data integrity, and create a flexible system for managing information.
Let's check your understanding of these foundational concepts.
What is a primary advantage of using a relational database with separate tables for customers and orders, compared to a single large spreadsheet?
In a database table like Customers, a single entry that represents a complete record for one person is called a _______.
With this structure in mind, you're ready to see how we can interact with and pull information from these databases.