← PostgreSQL learning pathPostgreSQL · Module 2 of 10
Creating tables and data types
Design tables with the right data types and constraints.
📖 Key ideas
- CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);
- Common types: INTEGER, BIGINT, NUMERIC, TEXT, VARCHAR(n), BOOLEAN, DATE, TIMESTAMPTZ, UUID.
- Constraints: PRIMARY KEY, NOT NULL, UNIQUE, CHECK, FOREIGN KEY.
- Use GENERATED ALWAYS AS IDENTITY for modern auto ids.
- 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