TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 14 of 14 · By Ethan Cole · Updated November 21, 2026

SQL CREATE TABLE: Columns, Types and Constraints

Every table you have queried was created with a CREATE TABLE statement. Designing a table means choosing its columns, their types, and the constraints that stop bad data from getting in. Constraints are your last line of defense: the application may have bugs, but the database will refuse a review with a rating of 11.

Columns and types

Each column has a name and a declared type. Standard types include INTEGER, REAL (floating point), TEXT and BLOB (binary data). Other databases add VARCHAR(n), DECIMAL, DATE, BOOLEAN and more.

SQLite is unusual: its typing is flexible. The declared type is a preference (a "type affinity"), and a column can technically hold any kind of value. typeof() reports what is actually stored, which the last query uses. Add STRICT after the closing parenthesis, in SQLite 3.37 and later, to enforce types the way other databases do.

Constraints

  • PRIMARY KEY identifies each row uniquely. In SQLite, an INTEGER PRIMARY KEY column is filled in automatically when you leave it out of an INSERT.
  • NOT NULL forbids missing values.
  • UNIQUE forbids duplicates, such as two accounts with the same email.
  • CHECK (rating BETWEEN 1 AND 5) rejects any row for which the expression is false.
  • DEFAULT supplies a value when an INSERT leaves the column out. It can be an expression in parentheses, like (date('now')).
  • REFERENCES books(id) declares a foreign key: each book_id must match an existing book.
SQLite · editable

Expected output

title | reviews | avg_rating
Learning SQL by Doing | 1 | 5.0
Paper Cranes | 2 | 4.5
typeof(rating) | typeof(comment)
integer | null

What the example shows

The new reviews table links to books. The second review passes NULL for comment explicitly, so the default is not used: defaults apply only to columns that are left out. The join then reports the number of reviews and the average rating per book. Try changing a rating to 7 and running again: the CHECK constraint rejects the insert with an error.

Foreign keys in SQLite

For backward compatibility, SQLite does not enforce foreign keys unless you run PRAGMA foreign_keys = ON; on each connection. Most other databases enforce them always. Turn them on in every application that uses SQLite.

Changing a table later

ALTER TABLE books ADD COLUMN isbn TEXT; adds a column, and DROP TABLE IF EXISTS reviews; deletes a table with all its data. Real projects record such changes as migrations, small numbered scripts kept with the code.

Common mistakes

  • Storing money as REAL; store integer cents or use a decimal type where available.
  • Leaving out NOT NULL on columns that must always have a value.
  • Forgetting that SQLite needs PRAGMA foreign_keys = ON to enforce REFERENCES.

Summary

  • CREATE TABLE name (column type constraints, ...) defines a table.
  • PRIMARY KEY, NOT NULL, UNIQUE, CHECK, DEFAULT and REFERENCES protect data quality.
  • SQLite types are flexible unless the table is STRICT; typeof() shows the stored type.

Quiz: CREATE TABLE and constraints

1. Which constraint rejects a rating of 9 when ratings must be 1 to 5?

A CHECK constraint rejects rows where its expression is false.

2. When is a column's DEFAULT value used?

An explicit NULL is stored as NULL; defaults fill omitted columns.

3. What must you run in SQLite to enforce foreign keys?

SQLite leaves foreign key enforcement off by default for compatibility.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.