Skip to main content

Lesson 3 of 4 15 min read

Databases and persistent data

Learn why applications need databases, how relational and document databases differ, and how a backend reads and writes data.

In this lesson you will

  • Explain persistence and why server memory is not enough
  • Compare relational (SQL) and document (NoSQL) databases at a high level
  • Describe how a backend uses queries to read and write data safely

Why persist data?

A backend keeps working data in memory while it runs, but memory is wiped whenever the server restarts or crashes. Applications also run on several servers at once, each with its own memory. To keep data safe, shared and searchable, applications store it in a database: software designed to save, organise and retrieve data reliably.

Data that survives restarts is called persistent data. User accounts, orders, course progress and messages are all persistent.

Relational databases

A relational database (PostgreSQL, MySQL, Oracle, SQL Server) stores data in tables with fixed columns, much like spreadsheets with strict rules:

idnameemail
1Ravi Kumarravi@example.com
2Asha Menonasha@example.com

Tables are linked with keys, and you query them with SQL (Structured Query Language):

SQL
SELECT name, email
FROM students
WHERE id = 2;

Relational databases enforce structure and constraints (for example, every email must be unique) and support transactions: a group of changes that either all succeed or all fail. That is essential for money transfers or seat bookings.

Document databases

A document database (MongoDB, Firestore) stores flexible JSON-like documents:

JSON
{
  "_id": "s2",
  "name": "Asha Menon",
  "email": "asha@example.com",
  "skills": ["HTML", "CSS"]
}

Documents can vary in shape, which is convenient for rapidly changing data. Both kinds of database are widely used; the Database Fundamentals path teaches SQL in depth.

How a backend talks to a database

The frontend never connects to the database directly. The backend receives a request, runs a query and returns the result:

The browser never talks to the database directly

Browser sends to Backend API: GET /api/students/3. Backend API sends to Database: SELECT … WHERE id = $1 (with 3). Database replies to Backend API: one row. Backend API replies to Browser: 200 OK + JSON

Here is a small Node.js and Express endpoint using the pg PostgreSQL library:

JavaScript
app.get("/api/students/:id", async (req, res) => {
  const result = await pool.query(
    "SELECT id, name, email FROM students WHERE id = $1",
    [req.params.id],
  );
  if (result.rows.length === 0) {
    return res.status(404).json({ message: "Student not found" });
  }
  res.json(result.rows[0]);
});

Notice the $1 placeholder. The id from the URL is passed separately from the SQL text, so a malicious value cannot change the query. Building SQL by joining strings with user input creates SQL injection vulnerabilities, one of the most common security flaws.

Hands-on exercise

Imagine a to-do list app. Sketch a table named tasks with at least four columns (choose names and types such as id, title, is_done and created_at). Write three example rows. Then write in plain English the query the backend would run when a user ticks off task number 3, and explain why the browser should not send that SQL itself.

Knowledge check

3 questions • 70% to pass • Practice only, no certificate

  1. 1.What happens to data held only in a server's memory when the server restarts?
  2. 2.Which feature guarantees that a group of database changes either all succeed or all fail?
  3. 3.Why does the example query use $1 instead of joining the id into the SQL string?
0 of 3 answered

Key concepts

  • Databases keep data persistent, shared across servers and searchable.
  • Relational databases use tables, keys, constraints and transactions, queried with SQL.
  • Document databases store flexible JSON-like documents.
  • Only the backend talks to the database, using parameterised queries to prevent SQL injection.