Skip to main content

How to Create a Table in SQLite with CREATE TABLE

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A new SQLite database is empty. Before it can hold anything, it needs a table: a name, and the columns every row will have. This guide creates a books table with CREATE TABLE and checks what was created, with real output from SQLite 3.54.1.

Syntax​

Syntax
CREATE TABLE <table> (
<column> <type>,
<column> <type>
);
PartWhat it means
<table>The new table's name
<column> <type>One column: its name, then its type, such as TEXT, INTEGER, REAL or BLOB
,Separates the columns. There's none after the last one
;Ends the statement

SQLite's CREATE TABLE documentation describes the command as the one that "is used to create a new table in an SQLite database."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as create_table.sql:

create_table.sql
CREATE TABLE books (
title TEXT,
author TEXT,
pages INTEGER
);

PRAGMA table_info(books);

The create_table.sql script in a macOS-style code window: a CREATE TABLE statement for a books table with the columns title TEXT, author TEXT and pages INTEGER, followed by PRAGMA table_info(books).

  • CREATE TABLE books names the new table books.
  • Each line inside the parentheses is one column: title and author hold text, pages holds whole numbers.
  • PRAGMA table_info(books) lists the table's columns, so you can see what was created.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read create_table.sql"

sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+-----+--------+---------+---------+------------+----+
| cid | name | type | notnull | dflt_value | pk |
+-----+--------+---------+---------+------------+----+
| 0 | title | TEXT | 0 | | 0 |
| 1 | author | TEXT | 0 | | 0 |
| 2 | pages | INTEGER | 0 | | 0 |
+-----+--------+---------+---------+------------+----+

The output in a terminal: PRAGMA table_info lists the books table's three columns, title and author with the type TEXT and pages with the type INTEGER, numbered 0 to 2, with notnull and pk both 0 and no default values.

library.db now holds a books table with three columns, numbered in the order they were written (cid 0 to 2), each with its type. The other columns are 0 or empty: none of the columns is required (notnull), has a default value (dflt_value) or belongs to the primary key (pk).

Common Mistakes​

MistakeWhat happensDo this instead
A missing comma, title TEXT author TEXT, pages INTEGERNo error at all: SQLite made one column, title, with the type TEXT author TEXT, and no author columnPut a comma after every column except the last
A comma after the last columnParse error ...: near ")": syntax errorRemove the comma before )
Expecting SQLite to check the typesAn INSERT put the text 'lots' into pages INTEGER, and it was stored as textAdd STRICT after the closing parenthesis, as in the FAQ below

Troubleshooting​

SymptomLikely causeFix
Parse error near line 1 of create_table.sql: table books already existsThe script already ran on library.dbDelete library.db and run the script again, or write CREATE TABLE IF NOT EXISTS books

FAQ​

Can I run the script again without an error?​

Yes. CREATE TABLE IF NOT EXISTS books (...) creates the table only when it isn't there yet, and does nothing otherwise: run twice, it gave no error.

How do I make SQLite check the column types?​

Write STRICT after the closing parenthesis: CREATE TABLE books (...) STRICT;. Then the same INSERT of 'lots' into pages failed with cannot store TEXT value in INTEGER column books.pages. STRICT tables allow only the types INT, INTEGER, REAL, TEXT, BLOB and ANY.

Which types can a column have?​

SQLite stores every value as one of five classes: NULL, INTEGER, REAL, TEXT and BLOB. A column's type is a preference, not a rule, unless the table is STRICT.

Conclusion​

CREATE TABLE books (...) gave library.db a table with three typed columns, and PRAGMA table_info(books) showed them in order, ready for rows.

Additional Resources​