Relational foundations
What relational databases store
Understand how SQL Server organizes related records in tables and why keys matter.
Lesson 1 of 84
Reading: about 3 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page3 sections
A relational database stores related facts in tables. A table describes one kind of thing, such as a customer or an order. Each row represents one record; each column represents one named attribute of that record.
A database is more than a spreadsheet
Our DDI_SQL_Training database has eight related tables. Customers has 20 customer records. Orders has 5,000 order records. They are separate because a customer can place many orders, while each order belongs to one customer.
This read-only T-SQL example uses DDI_SQL_Training dataset version 1.0.0 on SQL Server 2019 or later. You can read it now and run it after setting up the practice database in lesson 5. The next module explains SELECT in detail.
SELECT TOP (3) CustomerID, FirstName, City
FROM dbo.Customers
ORDER BY CustomerID;| CustomerID | FirstName | City |
|---|---|---|
| 1 | Alex | London |
| 2 | Maya | Manchester |
| 3 | Leo | Birmingham |
The query shows three of the 20 Customers rows. TOP (3) limits the result and ORDER BY CustomerID makes those three rows repeatable. The full table has no permanent display order; ask for one when it matters.
How tables connect
Customers.CustomerID identifies a customer. Orders.CustomerID records which customer placed an order. The shared key lets a query connect an order to its customer without repeating the customer name in every order row.
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 1 | 1 | 2023-01-02 |
| 21 | 1 | 2023-01-22 |
| 2 | 2 | 2023-01-03 |
Orders 1 and 21 both have CustomerID 1, so they refer to Alex's customer record. Order 2 has CustomerID 2, so it refers to Maya's record. Instead of repeating each customer's details in every order, the order can reference the customer using CustomerID.
Check your understanding
Which table should store the order date, and which should store the customer’s name?
Reveal answer
Orders stores OrderDate. Customers stores FirstName and LastName. The order refers to its customer through CustomerID.
Your progress
Reveal the check answer or reach the end to complete this lesson.