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
);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;| id | name | city | phone |
|---|---|---|---|
| 1 | Doi Coffee Co. | Chiang Mai | 053-111-222 |
| 2 | Bangkok Paper | Bangkok | 02-333-4444 |
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') |
|---|---|---|---|---|
| integer | real | text | null | blob |
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;| n | typeof(n) | t | typeof(t) |
|---|---|---|---|
| 42 | integer | 42 | text |
| abc | text | 7.5 | text |
| 3 | integer | NULL | null |
'42'went into anINTEGERcolumn and became the number 42;3.0became 3: nothing was lost.'abc'can't be a number, so it stayed text, in anINTEGERcolumn.- In the
TEXTcolumn, the numbers 42 and 7.5 became the texts'42'and'7.5'.NULLstaysNULLanywhere.
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;| name | price | typeof(price) |
|---|---|---|
| Coffee | 55.0 | real |
SQLite reads the type's name and decides its affinity by these rules, in order:
| The type's name has | Affinity | e.g. |
|---|---|---|
INT | INTEGER | INTEGER, INT, BIGINT |
CHAR, CLOB or TEXT | TEXT | TEXT, VARCHAR(50) |
BLOB, or no type | BLOB (kept as given) | BLOB |
REAL, FLOA or DOUB | REAL | REAL, DOUBLE |
| anything else | NUMERIC | NUMERIC, 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);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');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;| qty | typeof(qty) |
|---|---|
| 118 | integer |
| 120 | integer |
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);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 |
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;Dropping it again is an error, since it isn't there; IF EXISTS makes that quiet:
DROP TABLE trial;DROP TABLE IF EXISTS trial;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;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;| id | name | price |
|---|---|---|
| 1 | Coffee | 55.0 |
| 2 | Green Tea | 45.0 |
| 3 | Orange Juice | 40.0 |
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) |
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
- Small letters, words joined by
_:order_item,category_id. Names don't care about capitals, soProductandproductare the same table, and small letters everywhere is the simplest to read. - One way for everything: this shop names tables in the singular (
product,customer), and a column pointing at another table<table>_id. Pick a custom and keep it. - Not SQL's own words.
orderis one (ORDER BY), which is why the shop's table isorders:
CREATE TABLE order (id INTEGER PRIMARY KEY);- No spaces or signs, and English letters. A name with a space or a Thai name works only between double quotes (
"ลูกค้า"), every time it is used; that soon gets tiring. Keep Thai for the data, not the names.
- Make a
STRICTtablestaffwithid INTEGER PRIMARY KEY,name TEXT NOT NULLandsalary REAL, and add one person to it. - Add a second person whose salary is written as the text
'18000', and see withtypeof()what was kept. - Make a table
cheap_productfrom 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;| id | name | salary |
|---|---|---|
| 1 | Suda | 15000.0 |
| name | salary | typeof(salary) |
|---|---|---|
| Suda | 15000.0 | real |
| Krit | 18000.0 | real |
| name | price |
|---|---|
| Potato Chips | 25.0 |
| Pen | 15.0 |
| Highlighter | 25.0 |
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