Creating Tables

Learn how to create database tables and define their structure with proper data types and constraints.

Basic Table Creation

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    age INTEGER,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Data Types

TypeDescriptionExample
INTEGERWhole numbers42
TEXTText strings‘Hello’
REALDecimal numbers3.14
BOOLEANTrue/false valuesTRUE
DATEDate values‘2024-01-01’
TIMESTAMPDate and time‘2024-01-01 12:00:00’

Constraints

  • PRIMARY KEY: Unique identifier for each row
  • NOT NULL: Field cannot be empty
  • UNIQUE: Values must be unique across rows
  • DEFAULT: Default value if none specified
  • CHECK: Validate data before insertion
CREATE TABLE products (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    price REAL CHECK (price > 0),
    category TEXT DEFAULT 'general',
    in_stock BOOLEAN DEFAULT TRUE
);

Next Steps

Now that you have tables, learn how to perform CRUD operations to manipulate your data.

Last updated on