Module F-2·22 min read

Data types, CREATE TABLE, INSERT, SELECT, UPDATE, DELETE — every fundamental operation from first principles.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

F-2 — Tables, Rows, and the Relational Mental Model

Who this module is for: You completed F-1, have PostgreSQL installed, and can connect with psql. Now you need to understand what a table actually is, what data types exist and which to use, and how to perform every fundamental SQL operation — creating tables, inserting data, reading it, modifying it, and deleting it. No assumed knowledge of SQL.


The Relational Mental Model

A relational database stores data in tables. A table is the simplest structure to grasp: it is a spreadsheet.

text
  • A column (also called a field or attribute) defines a piece of data every row will have — its name and type
  • A row (also called a record or tuple) is one complete entity — one user, one order, one product
  • A cell is the intersection of a row and column — one specific value

The critical difference from a spreadsheet: in PostgreSQL, every value in a column must match that column's declared type. You cannot store the number 42 in a column declared as TEXT, and you cannot store the string "hello" in a column declared as INTEGER. The database enforces this for you.


Data Types — The Foundation of Every Column

Choosing the right data type for each column is one of the most important decisions you make. The wrong type causes subtle bugs, wastes storage, and breaks operations you will want to do later.

Numbers

sql

When to use which:

  • INTEGER — counts, ages, quantities, foreign key references (if table will stay under 2 billion rows)
  • BIGINT — auto-incrementing IDs in high-traffic systems, timestamps as milliseconds
  • NUMERIC(10, 2) — prices ($999,999.99), percentages, anything where exact decimal matters
  • Never use FLOAT or DOUBLE PRECISION for financial data — 0.1 + 0.2 is not exactly 0.3 in floating point

Text

sql

The rule: use TEXT for everything unless you have a specific reason to enforce a maximum length. VARCHAR(255) is a habit from older databases — in PostgreSQL, TEXT is just as efficient and more flexible.

True/False

sql

PostgreSQL accepts TRUE, FALSE, 't', 'f', 'yes', 'no', '1', '0' when inserting.

Dates and Times

sql

The most important rule in this module: always use TIMESTAMPTZ, never bare TIMESTAMP. A bare TIMESTAMP stores a date and time with no timezone context — if your server's timezone changes, or if you have users in multiple timezones, the stored values become ambiguous. TIMESTAMPTZ stores UTC internally and converts to/from the session timezone transparently.

Unique Identifiers

sql

For primary keys: use BIGSERIAL for most tables. Use UUID when you need IDs generated client-side or across distributed systems.


CREATE TABLE

sql

A complete example:

sql

Breaking this down:

  • id BIGSERIAL PRIMARY KEY — auto-incrementing integer, unique identifier for each row
  • name TEXT NOT NULL — required text field (cannot be left empty)
  • description TEXT — optional (can be NULL — the absence of a value)
  • price NUMERIC(10,2) NOT NULL — exact decimal, required
  • in_stock BOOLEAN NOT NULL DEFAULT true — required, defaults to true if not provided
  • created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() — automatically set to the current time

Naming conventions: use snake_case (lowercase with underscores). Do not use spaces, capital letters, or reserved words (order, user, table need to be quoted if used as names — avoid them).

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.