20. CREATE TABLE: making tables

Until now the shop's tables were there already. This chapter makes new ones: their columns, the type of each, and what SQLite really does with that type.

Making a table

CREATE TABLE makes a new, empty table: its name, then in brackets each column with its type, separated by commas. The shop wants to keep its suppliers:

CREATE TABLE supplier (
  id    INTEGER PRIMARY KEY,
  name  TEXT NOT NULL,
  city  TEXT,
  phone TEXT
);
✓ Done

The words after a column's type (PRIMARY KEY, NOT NULL) are rules, chapter 21's subject. The new table is empty until we add rows:

INSERT INTO supplier (name, city, phone) VALUES
  ('Doi Coffee Co.', 'Chiang Mai', '053-111-222'),
  ('Bangkok Paper',  'Bangkok',    '02-333-4444');
SELECT * FROM supplier;
idnamecityphone
1Doi Coffee Co.Chiang Mai053-111-222
2Bangkok PaperBangkok02-333-4444
2 rows

Types, and what SQLite does with them

Every value in SQLite is one of five kinds. typeof() says which:

SELECT typeof(42), typeof(4.2), typeof('42'), typeof(NULL), typeof(x'CAFE');
typeof(42)typeof(4.2)typeof('42')typeof(NULL)typeof(x'CAFE')
integerrealtextnullblob
1 row

integer (whole numbers), real (numbers with a fraction), text, null (no value) and blob (raw bytes, like a picture's).

Here SQLite differs from most databases: a column's type is a preference, not a rule. It is called the column's affinity: the database tries to turn a value into the column's type when that loses nothing, and keeps it as it is when it can't. A small table to see it:

CREATE TABLE trial (
  n INTEGER,
  t TEXT
);
INSERT INTO trial (n, t) VALUES ('42', 42), ('abc', 7.5), (3.0, NULL);
SELECT n, typeof(n), t, typeof(t) FROM trial;
ntypeof(n)ttypeof(t)
42integer42text
abctext7.5text
3integerNULLnull
3 rows

The shop's price is REAL, so the 55 that practice.sql gave the Coffee is kept as 55.0:

SELECT name, price, typeof(price) FROM product WHERE id = 1;
namepricetypeof(price)
Coffee55.0real
1 row

SQLite reads the type's name and decides its affinity by these rules, in order:

The type's name hasAffinitye.g.
INTINTEGERINTEGER, INT, BIGINT
CHAR, CLOB or TEXTTEXTTEXT, VARCHAR(50)
BLOB, or no typeBLOB (kept as given)BLOB
REAL, FLOA or DOUBREALREAL, DOUBLE
anything elseNUMERICNUMERIC, DECIMAL(10,2), DATE

VARCHAR(5) is just TEXT to SQLite: the 5 is not checked, and a text of any length goes in. There is no date type either: the shop keeps dates as text like '2026-12-01' (chapter 8). When a length or a type must be enforced, say so with a rule (CHECK, chapter 21) or a STRICT table (next).

STRICT tables: types that are enforced

Write STRICT after the closing bracket and the types become rules. A STRICT table's columns take only INTEGER (or INT), REAL, TEXT, BLOB or ANY:

CREATE TABLE stock_count (
  product_id INTEGER NOT NULL,
  counted    TEXT NOT NULL,
  qty        INTEGER NOT NULL
) STRICT;
INSERT INTO stock_count (product_id, counted, qty) VALUES (1, '2026-12-01', 118);
✓ 1 row changed

A value that can't be the column's type is refused, instead of slipping in:

INSERT INTO stock_count (product_id, counted, qty) VALUES (1, '2026-12-02', 'many');
Error: cannot store TEXT value in INTEGER column stock_count.qty

A value that turns into the type without losing anything is still accepted and turned: '120' goes into qty as the number 120:

INSERT INTO stock_count (product_id, counted, qty) VALUES (1, '2026-12-02', '120');
SELECT qty, typeof(qty) FROM stock_count;
qtytypeof(qty)
118integer
120integer
2 rows

For a new table, STRICT is a good habit: a mistake in a program shows up as an error the day it happens, not as odd data months later. A table made by Phai SQL with STRICT opens in SQLite (3.37 or later) with the same rules.

IF NOT EXISTS, and DROP TABLE

Making a table whose name is taken is an error:

CREATE TABLE supplier (id INTEGER PRIMARY KEY, name TEXT);
Error: table supplier already exists

IF NOT EXISTS makes it "make it only if it isn't there", with no error. Handy in a program's setup that runs every time it starts:

CREATE TABLE IF NOT EXISTS supplier (id INTEGER PRIMARY KEY, name TEXT);
SELECT count(*) FROM supplier;
count(*)
2
1 row

Nothing changed: the table with its 2 suppliers is as it was (and its columns are still the first table's, not the ones in this statement).

DROP TABLE deletes a table, with all its rows:

DROP TABLE trial;
✓ Done

Dropping it again is an error, since it isn't there; IF EXISTS makes that quiet:

DROP TABLE trial;
Error: no such table: trial
DROP TABLE IF EXISTS trial;
✓ Done

With foreign keys on, a table that other rows still point at can't be dropped. Products point at their categories, so:

DROP TABLE category;
Error: FOREIGN KEY constraint failed

DROP TABLE doesn't ask and can't be undone (outside a transaction, chapter 19). Before dropping, make sure it is the table you mean, and that nothing needs its rows any more.

CREATE TABLE ... AS SELECT

CREATE TABLE name AS SELECT ... makes a table from a result: the columns of the SELECT become its columns, and its rows are copied in. A list of the drinks:

CREATE TABLE drink_price AS
SELECT id, name, price FROM product WHERE category_id = 1;
SELECT * FROM drink_price;
idnameprice
1Coffee55.0
2Green Tea45.0
3Orange Juice40.0
3 rows

It copies the data, not the rules: the new table has no PRIMARY KEY, no NOT NULL, no DEFAULT. The database keeps every table's CREATE TABLE in sqlite_schema; this one shows what was really made:

SELECT sql FROM sqlite_schema WHERE name = 'drink_price';
sql
CREATE TABLE drink_price(id INT,name TEXT,price REAL)
1 row

So it is good for a quick copy or a snapshot (as chapter 17 did before an UPDATE with no WHERE); for a real table, write the CREATE TABLE with its rules and fill it with INSERT ... SELECT (chapter 16).

Naming tables and columns

CREATE TABLE order (id INTEGER PRIMARY KEY);
Error: near "order": syntax error
  1. Make a STRICT table staff with id INTEGER PRIMARY KEY, name TEXT NOT NULL and salary REAL, and add one person to it.
  2. Add a second person whose salary is written as the text '18000', and see with typeof() what was kept.
  3. Make a table cheap_product from the products under 30 baht (name and price), look at it, then drop it so that running the drop twice gives no error.
Show the answer
CREATE TABLE staff (id INTEGER PRIMARY KEY, name TEXT NOT NULL, salary REAL) STRICT;
INSERT INTO staff (name, salary) VALUES ('Suda', 15000);
SELECT * FROM staff;
INSERT INTO staff (name, salary) VALUES ('Krit', '18000');
SELECT name, salary, typeof(salary) FROM staff;
CREATE TABLE cheap_product AS SELECT name, price FROM product WHERE price < 30;
SELECT * FROM cheap_product;
DROP TABLE IF EXISTS cheap_product;
DROP TABLE IF EXISTS cheap_product;
idnamesalary
1Suda15000.0
1 row
namesalarytypeof(salary)
Suda15000.0real
Krit18000.0real
2 rows
nameprice
Potato Chips25.0
Pen15.0
Highlighter25.0
3 rows

Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made