Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

create-table-postgresql

CREATE TABLE in PostgreSQL

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches CREATE TABLE in PostgreSQL: the SQL patterns, schema habits, and query reasoning you need before advancing in PostgreSQL.

Teams query CREATE TABLE in PostgreSQL on every PostgreSQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply CREATE TABLE in PostgreSQL in contexts like: Modern startups, geospatial apps, and analytics-friendly OLTP systems.

Copy Postgres SQL into psql, local PostgreSQL, or DB Fiddle (PostgreSQL dialect)—use \d and EXPLAIN ANALYZE where lessons show them. The in-browser lab ships later; psql is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Postgres DDL resembles ANSI SQL with dialect additions: tablespaces, inheritance (legacy), and rich constraints. Always pair CREATE with explicit types and defaults.

Basic table

CREATE TABLE products (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  sku TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  price NUMERIC(12,2) NOT NULL CHECK (price >= 0),
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

Practice: Apply DDL in a throwaway practice database. Use \d table in psql to verify constraints and indexes.

Foreign keys

CREATE TABLE order_items (
  id BIGSERIAL PRIMARY KEY,
  order_id BIGINT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
  product_id BIGINT NOT NULL REFERENCES products(id),
  qty INT NOT NULL CHECK (qty > 0)
);

Alter safely

ALTER TABLE products ADD COLUMN archived BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE products ALTER COLUMN name SET NOT NULL;

Production changes go through reviewed migrations—see Django migrations for app-driven workflows.

Important interview questions and answers

  1. Q: GENERATED IDENTITY vs SERIAL?
    A: IDENTITY is standard SQL; SERIAL is legacy shorthand still common in tutorials.
  2. Q: ON DELETE CASCADE risk?
    A: Deleting parent removes children—verify business rules before enabling.

Self-check

  1. How do you add a NOT NULL column to a populated table safely?
  2. What does REFERENCES enforce?

Tip: Align Django migrations with the same engine you use in production.

Interview prep

IDENTITY vs SERIAL?

IDENTITY is SQL-standard; SERIAL is legacy shorthand.

FK ON DELETE CASCADE?

Deleting parent removes dependent child rows.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • IDENTITY vs SERIAL?
  • IF NOT EXISTS?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump