Lesson 2 of 14
8. SQL vs NoSQL

8.1 What are tables, rows and keys?

3 min read+10 XP

Relational databases

In the office: Each ledger has one subject (an authors ledger, a books ledger), ruled columns, and numbered lines. When the books ledger needs the author, it doesn't copy their details; it writes the author's line number.

In the system: A relational database stores data in tables.

  • A table holds one kind of thing: authors, books, orders.
  • A row is one item: one book. A column is one attribute with a type: title TEXT, price INTEGER.
  • A primary key (PK) uniquely identifies each row: books.id = 42. No two rows share it.
  • A foreign key (FK) is a column that points at another table's primary key: books.author_id = 1 means "see row 1 in authors". The database refuses a foreign key that points at nothing.

the bookstore as tables: authors, books, orders and order_items, linked by keys.

Figure: each fact lives in one place; keys link the tables together.

You ask questions in SQL (Structured Query Language), saying what you want and letting the database work out how:

sql

Examples: PostgreSQL, MySQL, SQLite, Oracle, SQL Server.

Memory code: "One table per thing, one row per item, keys for the links."