CREATE TABLE in SQL: building tables, explained and practiced
CREATE TABLE sets up a new table: its name, its columns, their data types and the rules every row has to follow. Good rules stop bad data at the door, long before it skews a report.
In short: CREATE TABLE suppliers (supplier_id INTEGER PRIMARY KEY, name TEXT NOT NULL) creates an empty table. Every column gets a name, a data type and, where needed, rules such as NOT NULL (required), DEFAULT (fallback value) or CHECK (allowed values).
What you should know first: reading data with SELECT and adding rows with INSERT.
Try it now
This time you build a table instead of querying one. After your statement runs, the exercise inserts test data and shows you what ended up in your table, so you can see straight away whether your required fields and defaults do their job.
Task Product categories are getting their own table. Create the table categories with the columns category_id (INTEGER PRIMARY KEY, the database assigns the number), name (text, required) and active (whole number, required, default 1). To check your table, the exercise then adds the categories from products (name only) and tries to save one category without a name and one without a value for active.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How CREATE TABLE works
After CREATE TABLE comes the name of the new table, then the columns in parentheses, separated by commas. Each column has a name, a data type and optional rules. Purchasing wants to keep track of suppliers:
CREATE TABLE suppliers (
supplier_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
payment_days INTEGER NOT NULL DEFAULT 30
);
INSERT INTO suppliers (name, city) VALUES ('Northern Paper Mills', 'Albany');
INSERT INTO suppliers (name, city, payment_days) VALUES ('Fenwick Office Furniture', 'Birmingham', 14);
SELECT * FROM suppliers;
| supplier_id | name | city | payment_days |
|---|---|---|---|
| 1 | Northern Paper Mills | Albany | 30 |
| 2 | Fenwick Office Furniture | Birmingham | 14 |
The database handed out the IDs 1 and 2 on its own, because supplier_id is an INTEGER PRIMARY KEY. The first insert left out the payment terms, so DEFAULT 30 filled them in. A supplier without a name would have been rejected.
The rules you'll use most
| Rule | What it does | Example |
|---|---|---|
NOT NULL | required field, empty values are rejected | name TEXT NOT NULL |
DEFAULT | value used when the insert doesn't supply one | status TEXT DEFAULT 'open' |
PRIMARY KEY | unique identifier for each row | customer_id INTEGER PRIMARY KEY |
UNIQUE | each value may appear only once | email TEXT UNIQUE |
CHECK | condition every row must meet | price REAL CHECK (price >= 0) |
One subtlety with CHECK: if the condition comes out as NULL because the value is missing, the SQLite and PostgreSQL docs both treat that as passing. When a value is mandatory, add NOT NULL as well. Primary and foreign keys get their own lesson: primary and foreign keys.
One more exercise: rules with CHECK
Task Marketing is planning discount campaigns. Create the table promotions: promotion_id (INTEGER PRIMARY KEY), product_id (whole number, required), discount (whole number, required, only 1 to 50 percent allowed) and status (text, required, default 'planned'). To check it, the exercise adds a 10 percent promotion for every product with more than 100 units in stock, then tries discounts of 0, 50 and 60 percent and one with no value.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Data types in MySQL, PostgreSQL, SQL Server and SQLite
Data types are where the databases differ the most. Here are the everyday cases:
| Content | MySQL | PostgreSQL | SQL Server | SQLite |
|---|---|---|---|---|
| whole number | INT | integer | int | INTEGER |
| money amount | DECIMAL(10,2) | numeric(10,2) | decimal(10,2) | NUMERIC or REAL |
| text | VARCHAR(100) | varchar(100) or text | nvarchar(100) | TEXT |
| date | DATE | date | date | text as YYYY-MM-DD |
| yes/no | BOOLEAN (same as TINYINT(1)) | boolean | bit | number 0 or 1 |
| auto-numbered ID | INT AUTO_INCREMENT | GENERATED ALWAYS AS IDENTITY | IDENTITY(1,1) | INTEGER PRIMARY KEY |
SQLite is relaxed about types. It has no separate date or yes/no type, and the type name you write only tells it how it should prefer to store values. VARCHAR(100) is treated like TEXT, and SQLite doesn't enforce the length. Strict type checking arrived with STRICT tables in version 3.37.0. In the other three databases the type decides what can be stored at all, so pick types and lengths with care.
IF NOT EXISTS: create it only if it's missing
If the table already exists, CREATE TABLE stops with an error. With CREATE TABLE IF NOT EXISTS suppliers (...) nothing happens instead, which is handy for scripts that run more than once. The catch: the existing table isn't compared with your definition. If it has different columns, it stays exactly as it was.
A table from a query: CREATE TABLE … AS SELECT
Instead of listing columns, you can build a table straight from a query result. The columns take their names from the SELECT:
CREATE TABLE out_of_stock AS
SELECT product_id, name, price
FROM products
WHERE stock = 0;
SELECT * FROM out_of_stock;
| product_id | name | price |
|---|---|---|
| 7 | Yellow sticky notes, 12 pads | 6.99 |
| 12 | Cross-cut shredder | 129.0 |
What you get is a snapshot: once the warehouse restocks, the new table doesn't change. In SQLite it also has no primary key and no rules at all. If you want something that always reflects the current data, a view is the right tool.
Task Accounting wants to keep a fixed snapshot of the cancelled orders. Use CREATE TABLE … AS SELECT to create the table cancelled_orders with the columns order_id, customer_id, order_date and order_value (the sum of quantity times unit_price over all items) for every order with the status cancelled.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 39 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
| 1013 | 2 | 5 | 3.49 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Common mistakes
A comma after the last column
active INTEGER NOT NULL DEFAULT 1,) fails because the comma promises another column. The exercise reports “Syntax error near ")"”. The last column is followed only by the closing parenthesis.
A required column with no default
If active has NOT NULL but no DEFAULT, every insert that leaves active out fails. The exercise then says “categories.active must not be empty (NOT NULL)”. In this case it means: the test inserts don't supply a value, so the column needs a default.
The table already exists
Creating customers a second time gives you “customers already exists. Use another name or write IF NOT EXISTS.” To change an existing table, use ALTER TABLE.
INT instead of INTEGER for the ID
In SQLite only the exact spelling INTEGER PRIMARY KEY counts up by itself. With INT PRIMARY KEY the ID stays empty when the insert leaves it out, and the exercise tells you that not all values match.
Default text in double quotes
SQLite lets DEFAULT "planned" through, but PostgreSQL and SQL Server read double quotes as a column name. The exercise flags it: text goes in single quotes, DEFAULT 'planned'.
And how do I create a database?
In MySQL, PostgreSQL and SQL Server, CREATE DATABASE shop; creates an empty database, and you then create the tables inside it. MySQL accepts IF NOT EXISTS there, PostgreSQL doesn't. SQLite has no CREATE DATABASE: a complete database with all its tables lives in a single file.
CREATE TABLE in MySQL, PostgreSQL, SQL Server and SQLite
| Database | IF NOT EXISTS | Table from a query | CHECK enforced |
|---|---|---|---|
| MySQL | yes | CREATE TABLE new AS SELECT … (AS is optional) | since 8.0.16, ignored before |
| PostgreSQL | yes, with a notice instead of an error | CREATE TABLE new AS SELECT … | yes |
| SQL Server | not part of the CREATE TABLE syntax | SELECT … INTO new FROM … | yes |
| SQLite (these exercises) | yes | CREATE TABLE new AS SELECT … | yes |
SQL Server's SELECT … INTO doesn't carry over indexes or constraints from the source table either.
Sources: MySQL Reference Manual (CREATE TABLE, CHECK Constraints, Numeric Type Syntax, CREATE DATABASE), PostgreSQL documentation (CREATE TABLE, Data Types, Constraints, CREATE DATABASE), Microsoft Learn (CREATE TABLE, Data types, SELECT INTO, CREATE DATABASE), sqlite.org (CREATE TABLE, Datatypes, FAQ, About, Release History 3.37.0).
Frequently asked questions
How do I delete a table?
With DROP TABLE suppliers;. The table and all its data are gone, with no confirmation prompt. In the exercises that's harmless because every run starts from fresh data. On a real database, double-check what you're dropping first.
Can I add columns later?
Yes, with ALTER TABLE ... ADD COLUMN, which is the next lesson. You don't have to know every column on day one.
Why are dates stored as text in the exercises?
SQLite has no date type of its own. Its date functions work with text in the form YYYY-MM-DD, and in that form dates also sort and compare correctly as text.
How should I name tables and columns?
Lowercase, no spaces, words joined with underscores: customer_since rather than “Customer Since”. Then you never need quotes around names, and your queries run on any database.