Database Fundamentals in Python: What are Databases and Why We Need Them

A thousand users sit in a Python list. A request comes in — "find the one whose email is such-and-such" — and the program dutifully scans the list from the top, record by record. At a thousand you don't notice; at a million it's a visible delay. And the list also vanishes on restart, and two people can't write to it at once. Fast lookup, data that survives a restart, concurrent access — that's the job of a database.

Almost any application that stores something for the long term — users, orders, messages — keeps it in a database, not in the program's memory. Let's figure out what a database is and why an ordinary Python list won't do for the job.

What is a Database?

Simple analogy: a database is like a smart warehouse. Each "shelf" has its own address, and the "warehouse robot" (DBMS) can quickly find and deliver any needed information.

Why Not Store Data in Python Lists?

Let's consider a simple example — storing users:

Python 3.13
# Storage in Python list
users = [
    {"id": 1, "name": "Anna", "email": "anna@example.com"},
    {"id": 2, "name": "Peter", "email": "peter@example.com"},
    # ... imagine a million users here
]

# Search user by email
def find_user_by_email(email):
    for user in users:  # Scan the ENTIRE list!
        if user["email"] == email:
            return user
    return None

Problems with this approach:

  • Slow search — need to check every user
  • Memory limitations — all data must fit in RAM
  • Data loss — if the program crashes, everything is lost
  • Concurrency — what if multiple users modify data simultaneously?

Databases solve all these problems out of the box — that's why they became the standard for serious applications.

Main Types of Databases

There are many types of databases, but let's highlight the three most important:

1. Relational Databases

Organize data into tables with clear structure. The most popular type.

-- Example users table
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(150)
);

2. Key-Value Databases

Store simple "key-value" pairs. Very fast for simple operations.

Python 3.13
# Example in Redis — popular key-value DB
user_session = {
    "session:user123": "logged_in",
    "cart:user123": "[1,5,9]",  # Product IDs in cart
    "last_seen:user123": "2024-01-15 10:30"
}

3. Document-Oriented Databases

Store data as documents (JSON format). Flexible structure.

Python 3.13
# Example user document
{
    "name": "Anna",
    "email": "anna@example.com",
    "preferences": {
        "theme": "dark",
        "language": "en"
    }
}

Popular Database Management Systems

Relational databases are what you'll run into most often: PostgreSQL and MySQL on servers, SQLite inside phones, browsers, and Python itself. Of the rest, you'll regularly meet Redis (caching and sessions) and MongoDB (documents).

The module's remaining three chapters are about relational databases, so from here on we'll talk only about them.

Understanding check

Why do apps store users in a database rather than a plain in-memory list?

What's Next?

In the next article, we'll start practicing with SQLite — the perfect database for learning, which is already built into Python. You'll create your first DB and learn to work with real data.