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
| Type | Description | Example |
|---|---|---|
INTEGER | Whole numbers | 42 |
TEXT | Text strings | ‘Hello’ |
REAL | Decimal numbers | 3.14 |
BOOLEAN | True/false values | TRUE |
DATE | Date values | ‘2024-01-01’ |
TIMESTAMP | Date and time | ‘2024-01-01 12:00:00’ |
Constraints
PRIMARY KEY: Unique identifier for each rowNOT NULL: Field cannot be emptyUNIQUE: Values must be unique across rowsDEFAULT: Default value if none specifiedCHECK: 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