Schema & Performance · 9 min read · Beginner
Tables, Constraints & Indexes
Design reliable schemas with CREATE TABLE, PRIMARY KEY, NOT NULL, UNIQUE, CHECK constraints, and speed up lookups with CREATE INDEX.
What you'll learn
- Enforce data quality at the database level with NOT NULL, UNIQUE, and CHECK
- Speed up frequent WHERE and JOIN lookups using CREATE INDEX
Good schemas prevent bad data from ever entering your database:
PRIMARY KEY: Uniquely identifies every row.NOT NULL: Forbids missing values.UNIQUE: Prevents duplicate emails or slugs.CHECK (...): Enforces domain rules likeCHECK (score BETWEEN 0 AND 100).CREATE INDEX: Builds a fast B-tree lookup structure soWHERE email = ?stays instant even on millions of rows.
Fill-in-the-Blank Exercise
Enforce Unique Emails & Add Index
Prevent duplicate emails and index the course_id foreign key.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL
);
CREATE idx_enrollments_course ON enrollments(course_id);Try It Yourself
SQLSchema Constraints & EXPLAIN QUERY PLAN
Query Result (In-Browser SQLite)
Click Run SQL to execute this query against a live in-browser SQLite database.
Check Your Understanding
Score: 0 / 1
1. Which constraint ensures a numeric score column stays between 0 and 100?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.