Databases SQL and Prisma Fundamentals
Introduction to Relational Databases
What is a Relational Database?
At its heart, a relational database is a system for storing and retrieving data that sees the world in terms of related information. Think of it like a library's card catalog, but far more powerful. Instead of just one big list, it uses a collection of organized lists, called tables, to keep track of everything. The real magic is how it connects, or relates, the information between these lists.
The main purpose of a relational database is to organize data into a structured format that minimizes redundancy and makes it easy to access and manage related information.
The Building Blocks
Every relational database is built from a few simple components. Let's break them down.
Tables: A table is a collection of related data held in a structured format. It's like a single spreadsheet in a workbook. You might have one table for Customers, another for Products, and a third for Orders.
Columns: A column represents a specific attribute or piece of information for every item in the table. In a Customers table, you might have columns for CustomerID, FirstName, LastName, and Email.
Rows: A row, also called a record, represents a single entry in a table. Each row in the Customers table would contain the information for one specific customer.
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 1 | Maria | Anders | maria.a@example.com |
| 2 | Ana | Trujillo | ana.t@example.com |
| 3 | Antonio | Moreno | antonio.m@example.com |
Connecting the Dots
Having separate tables is great for organization, but how do we link them? How do we know which customer placed a particular order? This is where keys come in.
Primary Key
noun
A column (or set of columns) in a table that uniquely identifies each row. No two rows can have the same primary key, and it cannot be empty (NULL).
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.
Here's what an Orders table might look like. Notice how the CustomerID column connects each order to a specific customer from the table above.
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 10248 | 3 | 2024-07-04 |
| 10249 | 1 | 2024-07-05 |
| 10250 | 1 | 2024-07-08 |
This relationship allows us to easily find all orders placed by Maria Anders (CustomerID 1) or to find out which customer placed order 10248 (Antonio Moreno, CustomerID 3). This system of keys is the foundation of the relational model.
These links establish different kinds of relationships. The one between customers and orders is a one-to-many relationship: one customer can have many orders, but each order belongs to only one customer. Other relationship types exist, but this is the most common one you'll encounter.
Let's check your understanding of these core concepts.
In a relational database, what is the primary role of a table?
A ____ represents a single entry (or record) in a table, while a ____ represents a specific attribute for every entry.
By organizing data into tables and linking them with keys, relational databases provide a reliable and efficient way to manage complex information.
