Web Development · Module 11 · Lesson 11 min read

Databases for Web Apps

Where your app's data lives

Browsers forget; databases remember. Users, posts, orders and messages belong in a database on the server.

SQL vs NoSQL

SQL (PostgreSQL, MySQL, SQLite)NoSQL (MongoDB, Firebase, Redis)
Shapetables with a fixed schemaflexible documents or key-value
Relationsjoins and foreign keysembedded or duplicated data
Best formost apps: users, orders, anything relationalflexible data, caching, real-time feeds

💡 For most new web apps, PostgreSQL is a safe default. Supabase gives you a hosted PostgreSQL with an API, authentication and storage.

Core SQL

SQL
SELECT id, title FROM posts WHERE published = true ORDER BY created_at DESC LIMIT 10;
INSERT INTO posts (title, author_id) VALUES ('Hello', 7);
UPDATE posts SET title = 'Hi' WHERE id = 1;
DELETE FROM posts WHERE id = 1;

Good habits

  • Always use parameters (placeholders), never string concatenation, to prevent SQL injection.
  • Add indexes on columns you filter or join on often.
  • Use foreign keys to keep data consistent.
  • Store timestamps in UTC (timestamptz).
  • Paginate big lists (LIMIT / OFFSET or cursor-based).
  • ORMs and query builders (Prisma, Drizzle, Knex) add type safety and migrations.

Example

SQL
-- A tiny blog schema (PostgreSQL / Supabase)
create table authors (
  id bigint generated always as identity primary key,
  name text not null,
  email text unique not null
);

create table posts (
  id bigint generated always as identity primary key,
  author_id bigint not null references authors (id) on delete cascade,
  title text not null,
  body text,
  published boolean not null default false,
  created_at timestamptz not null default now()
);

create index on posts (author_id);

-- Latest published posts with author names
select p.title, a.name as author, p.created_at
from posts p
join authors a on a.id = p.author_id
where p.published
order by p.created_at desc
limit 10;