← PostgreSQL learning path
PostgreSQL · Module 2 of 10

Creating tables and data types

Design tables with the right data types and constraints.

📖 Key ideas

  1. CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);
  2. Common types: INTEGER, BIGINT, NUMERIC, TEXT, VARCHAR(n), BOOLEAN, DATE, TIMESTAMPTZ, UUID.
  3. Constraints: PRIMARY KEY, NOT NULL, UNIQUE, CHECK, FOREIGN KEY.
  4. Use GENERATED ALWAYS AS IDENTITY for modern auto ids.
  5. Use NUMERIC for money, not FLOAT.
💡 Real-life example: Data types are container shapes: liquids in bottles, grains in sacks; the right shape avoids spills.
PRIMARY KEYUnique row identifier
TIMESTAMPTZTimestamp with time zone
CHECKRule a value must satisfy
IDENTITYAuto-incrementing column