Database Basics · Lesson 4 of 112

Database Kinds of Databases

What you will learn

  • The four common families: relational, document, key-value and graph
  • How the same facts look in each
  • What each family is good for
  • Why this course starts with relational

1. One idea, several shapes

Data can be organised in different ways. Here are the same facts, a customer called Rina in Dhaka who ordered a shirt and a scarf, in four shapes.

Relational: tables

import sqlite3, json

con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT, city TEXT)")
con.execute("INSERT INTO customers VALUES (1,'Rina','Dhaka'),(2,'Sam','Khulna')")
print(con.execute("SELECT * FROM customers").fetchall())
[(1, 'Rina', 'Dhaka'), (2, 'Sam', 'Khulna')]

Rows and columns, with the same columns for every row, linked by keys. Examples of systems: SQLite, PostgreSQL, MySQL.

Document: nested records

doc = {"name": "Rina", "city": "Dhaka", "orders": [{"item": "shirt", "price": 450}, {"item": "scarf", "price": 120}]}
print(json.dumps(doc))
{"name": "Rina", "city": "Dhaka", "orders": [{"item": "shirt", "price": 450}, {"item": "scarf", "price": 120}]}

Each record is one self-contained document (often JSON-like), and the orders are nested inside the customer. Different documents may have different fields. MongoDB is a document database.

Key-value: a giant dictionary

kv = {"session:abc": "rina", "session:xyz": "sam"}
print(kv["session:abc"])
rina

You store a value under a key and fetch it by the key. Very simple and very fast for lookups, but you cannot easily ask "all sessions older than an hour" unless you design for it.

edges = [("Rina","Sam"),("Sam","Lee"),("Lee","Amy")]
friends = {}
for a, b in edges:
    friends.setdefault(a, []).append(b)
print(friends)
{'Rina': ['Sam'], 'Sam': ['Lee'], 'Lee': ['Amy']}

The links (friend of, follows, road to) are the main data. Graph databases are built to follow chains of links quickly.

2. What each is good at

Family Good for Weaker at
Relational many linked kinds of data, rules, reporting very flexible or deeply nested data
Document records with varying shape, read as a whole linking records across collections
Key-value caching, sessions, counters questions across many keys
Graph networks, routes, recommendations plain tabular reporting

Real systems blur the lines: several relational systems can also store JSON, and several document systems support limited joins. Chapter 9 compares them properly.

3. Why we start with relational

  • The relational model is the oldest and most widely taught, and SQL works across many systems.
  • Its ideas (keys, normalization, transactions, indexes) appear in other families too.
  • It handles a large share of everyday applications.

Worked example: pick a family

Need Likely family
Customers, orders, payments that must always add up relational
A blog where each post has different extra fields document
Remember who is logged in, for 30 minutes key-value
"Friends of friends who like cycling" graph

These are first guesses, not rules. The data and the questions decide. All names are made up.

Common mistakes

  • Choosing by fashion. Choose by data shape and the questions you will ask.
  • Thinking "NoSQL" means "no rules" or "better". It means "not only SQL" and covers several different designs.
  • Thinking you must pick one family forever. Many applications use two or three together.

Key takeaways

  • Relational stores tables; document stores nested records; key-value stores values under keys; graph stores links.
  • Each family has strengths and weaknesses.
  • Relational is the best starting point and the focus of this course.

Practice

1. Which family stores nested records such as a customer with their orders inside?

Show answer

Document.

2. Which family is like a giant dictionary?

Show answer

Key-value.

3. Why is it hard to ask "all sessions older than an hour" in a plain key-value store?

Show answer

You can only look up by key; asking across many values needs extra structure that you must design yourself.

4. Which family would you consider for finding routes between cities?

Show answer

Graph, because the links between places are the main data.

5. Is "NoSQL" one kind of database?

Show answer

No. It is a label for several designs that are not purely relational.

6. Why does this course begin with relational databases?

Show answer

Its ideas are widely used, SQL works across many systems, and it fits many everyday applications.