SQL CREATE TABLE
CREATE TABLE defines a new table: its name, its columns, each column's data type and any rules (constraints) the data must follow.
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);Note: Most server databases also have
CREATE DATABASE name;to create a whole new database. SQLite has no such statement — a SQLite database is a single file, created when you open it.
A first table
TAD Market wants to collect product reviews:
CREATE TABLE reviews (
review_id INTEGER PRIMARY KEY,
product_id INTEGER NOT NULL,
customer_id INTEGER NOT NULL,
stars INTEGER NOT NULL,
comment TEXT,
created TEXT
);
INSERT INTO reviews (product_id, customer_id, stars, comment, created) VALUES
(1, 6, 5, 'Rich and smooth', '2025-06-01'),
(22, 2, 4, 'Loud but great to type on', '2025-08-20');
SELECT * FROM reviews;2 rows affected.
review_id | product_id | customer_id | stars | comment | created
----------+------------+-------------+-------+---------------------------+-----------
1 | 1 | 6 | 5 | Rich and smooth | 2025-06-01
2 | 22 | 2 | 4 | Loud but great to type on | 2025-08-20
(2 rows)
CREATE TABLE itself prints nothing; the INSERT and SELECT show that the table works.
Inspecting a table
SQLite stores every table definition in the sqlite_schema table, and pragma_table_info lists a table's columns:
SELECT name, type, "notnull", dflt_value, pk
FROM pragma_table_info('products');name | type | notnull | dflt_value | pk
------------+---------+---------+------------+---
product_id | INTEGER | 0 | NULL | 1
name | TEXT | 1 | NULL | 0
category_id | INTEGER | 0 | NULL | 0
supplier_id | INTEGER | 0 | NULL | 0
unit | TEXT | 0 | NULL | 0
price | REAL | 1 | NULL | 0
stock | INTEGER | 1 | 0 | 0
(7 rows)
| Database | Describe a table |
|---|---|
| SQLite | PRAGMA table_info(t) or SELECT … FROM pragma_table_info('t') |
| MySQL | DESCRIBE t; |
| PostgreSQL | \d t in psql, or information_schema.columns |
| SQL Server | EXEC sp_help 't' |
Common data types
| Kind | SQLite | Other databases |
|---|---|---|
| Whole numbers | INTEGER |
INT, BIGINT, SMALLINT |
| Decimals | REAL |
DECIMAL(10,2), NUMERIC, FLOAT, DOUBLE |
| Text | TEXT |
VARCHAR(255), CHAR(10), TEXT |
| Dates | TEXT (ISO strings) |
DATE, TIMESTAMP, DATETIME |
| Binary | BLOB |
BLOB, BYTEA, VARBINARY |
| True/false | INTEGER 0/1 |
BOOLEAN, BIT |
See the Data Types lesson for details.
IF NOT EXISTS
Creating a table that already exists is an error. IF NOT EXISTS makes the statement do nothing instead — useful in setup scripts that may run twice:
CREATE TABLE IF NOT EXISTS categories (id INTEGER);
SELECT COUNT(*) AS categories FROM categories;
CREATE TABLE categories (id INTEGER);categories
----------
8
(1 row)
Error: table categories already exists
Constraints at a glance
Constraints are rules attached to columns or to the whole table. Each has its own lesson:
CREATE TABLE coupons (
code TEXT PRIMARY KEY,
percent_off INTEGER NOT NULL CHECK (percent_off BETWEEN 1 AND 90),
customer_id INTEGER REFERENCES customers(customer_id),
valid_until TEXT NOT NULL,
used INTEGER NOT NULL DEFAULT 0,
UNIQUE (customer_id, valid_until)
);
INSERT INTO coupons (code, percent_off, customer_id, valid_until)
VALUES ('WELCOME10', 10, 13, '2025-12-31');
SELECT * FROM coupons;
INSERT INTO coupons (code, percent_off, valid_until) VALUES ('HUGE', 95, '2025-12-31');1 row affected.
code | percent_off | customer_id | valid_until | used
----------+-------------+-------------+-------------+-----
WELCOME10 | 10 | 13 | 2025-12-31 | 0
(1 row)
Error: CHECK constraint failed: percent_off BETWEEN 1 AND 90
The second insert breaks the CHECK rule and is rejected.
Creating a table from a query
CREATE TABLE new AS SELECT … creates and fills a table in one go — see the SELECT INTO lesson.
Test yourself
What does CREATE TABLE IF NOT EXISTS t (...) do when t already exists?
- Replaces t
- Raises an error
- Does nothing
- Adds the new columns
How do you list the columns of a table in SQLite?
- DESCRIBE t
- SHOW COLUMNS t
- PRAGMA table_info(t)
- sp_help t