Meet relational data
Read tables, rows, columns and keys as the building blocks of a database.
- Describe how a relational database organizes connected information.
A relational database stores data in tables. Each row is one record, and each column describes one kind of value. A primary key identifies a row; a foreign key stores a reference to a row in another table.
1CREATE TABLE customers (
2 customer_id INTEGER PRIMARY KEY,
3 name TEXT NOT NULL,
4 city TEXT
5);
6
7CREATE TABLE orders (
8 order_id INTEGER PRIMARY KEY,
9 customer_id INTEGER REFERENCES customers(customer_id),
10 total DECIMAL(10, 2)
11);The example uses familiar SQL types and constraints. Database products vary in their exact type names and syntax, but tables, columns and key relationships are common relational ideas.
Key takeaways
Describe how a relational database organizes connected information.
Use table and column names that make the query easy to read.
Check which rows a query affects before relying on its result.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write SQL queries
Write queries against small sample databases and run them locally in your browser. Each test starts from a fresh SQLite database.
Find customers and their cities
Return the name and city for every customer, ordered alphabetically by name. The fixture includes three customers.
- Customer list
Sample tables and rows
Each test rebuilds an in-memory database for the selected dialect before running your query.
CREATE TABLE customers (customer_id INTEGER PRIMARY KEY, name TEXT, city TEXT); INSERT INTO customers VALUES (1, 'Sam Lee', 'Boston'), (2, 'Ari Chen', 'Boston'), (3, 'Jo Diaz', 'Salem');Using SQLite 3 (sql.js), an embedded database. Queries run locally in a worker against a fresh in-memory database for each test.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…