Creating tables in SQLite3 is fundamental to database design and involves using the CREATE TABLE statement with specific syntax and data types.
Understanding the proper structure and options will help you build robust database schemas.
Basic CREATE TABLE Syntax
The fundamental syntax for creating a table in SQLite3 follows this pattern:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
column3 datatype constraints
);
SQLite3 supports several data types including INTEGER, TEXT, REAL, BLOB, and NULL. Unlike other database systems, SQLite uses dynamic typing, meaning you can store any type of data in any column, though it's best practice to respect declared types.
Common Data Types and Examples
Here's a practical example creating a comprehensive users table:
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL,
age INTEGER,
salary REAL,
is_active INTEGER DEFAULT 1,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
This example demonstrates several important concepts: INTEGER PRIMARY KEY AUTOINCREMENT creates an auto-incrementing unique identifier, NOT NULL ensures required fields, UNIQUE prevents duplicate values, DEFAULT sets default values, and CURRENT_TIMESTAMP automatically timestamps records.
Column Constraints
SQLite3 supports various constraints to maintain data integrity:
- PRIMARY KEY: Uniquely identifies each row
- NOT NULL: Prevents empty values
- UNIQUE: Ensures no duplicate values
- DEFAULT: Sets default values for new records
- CHECK: Validates data against conditions
- FOREIGN KEY: Creates relationships between tables
Example with CHECK constraint:
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price REAL CHECK(price > 0),
category TEXT DEFAULT 'general'
);
Creating Tables with Foreign Keys
To establish relationships between tables, use FOREIGN KEY constraints:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER,
order_date TEXT DEFAULT CURRENT_TIMESTAMP,
total REAL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
Note that foreign key support must be enabled in SQLite3 using PRAGMA foreign_keys = ON;
Advanced CREATE TABLE Options
SQLite3 offers several advanced options:
IF NOT EXISTS: Prevents errors if the table already exists
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY,
message TEXT,
timestamp TEXT DEFAULT CURRENT_TIMESTAMP
);
TEMPORARY tables: Exist only for the current session
CREATE TEMPORARY TABLE temp_calculations (
id INTEGER,
result REAL
);
Creating Tables from Query Results
You can create and populate tables simultaneously:
CREATE TABLE active_users AS
SELECT * FROM users WHERE is_active = 1;
Best Practices
When creating tables, follow these guidelines: always define a PRIMARY KEY for efficient indexing, use meaningful column names, choose appropriate data types even though SQLite is flexible, include NOT NULL constraints for required fields, and consider adding indexes for frequently queried columns.
Practical Command-Line Example
Using the sqlite3 command-line tool:
sqlite3 mydatabase.db
sqlite> CREATE TABLE contacts (
...> id INTEGER PRIMARY KEY,
...> name TEXT NOT NULL,
...> phone TEXT UNIQUE
...> );
sqlite> .schema contacts
The .schema command shows the table structure, while .tables lists all tables in the database.
Understanding CREATE TABLE syntax is essential for effective SQLite3 database design, enabling you to build structured, reliable data storage solutions for your applications.