CREATE TABLE & Data Types
9 min read
Defining Tables
CREATE TABLE defines the structure of a new table — its columns, data types, and constraints. Choosing the right data types upfront saves a lot of pain later.
Common Data Types
INTEGER/INT— whole numbersNUMERIC(p,s)/DECIMAL(p,s)— exact decimal numbers (use for money)FLOAT/REAL/DOUBLE— approximate floating-point (avoid for money)TEXT/VARCHAR(n)— variable-length stringsCHAR(n)— fixed-length strings (padded with spaces)BOOLEAN— true/falseDATE— calendar date (YYYY-MM-DD)TIMESTAMP— date + time (with optional timezone)
sql
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title VARCHAR(200) NOT NULL,
category VARCHAR(100),
price NUMERIC(8,2) NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);Always use NUMERIC or DECIMAL for monetary values. Floating-point types like FLOAT cannot represent all decimal fractions exactly, leading to rounding errors in financial calculations.
Ready to test yourself?
Practice SQL— quiz & coding exercises