Lesson 22 of 30 · Designing Tables
SQL CREATE TABLE: Columns, Data Types and Primary Keys
Until now you've worked with tables someone else designed. Now you'll build your own. CREATE TABLE defines a new table: its name, its columns, and what kind of data each column holds.
CREATE TABLE
CREATE TABLE taverns (
id INTEGER PRIMARY KEY,
name TEXT,
city TEXT,
rooms INTEGER,
ale_price REAL
);
Each line inside the parentheses defines one column: a name, then a type. Columns are separated by commas, with no comma after the last one. By convention, table names are plural (taverns) and column names are lowercase with underscores (ale_price).
SQLite data types
| Type | Holds | Examples |
|---|---|---|
INTEGER |
whole numbers | level, gold, ids |
REAL |
decimal numbers | 2.5, ale_price |
TEXT |
text, including dates written as 'YYYY-MM-DD' |
name, joined |
BLOB |
raw bytes, such as files | rarely needed |
Any column can also hold NULL ("no value") unless you forbid it, which is the topic of the next lesson.
SQLite is relaxed about types: if you insist, it stores text in an INTEGER column. PostgreSQL, MySQL and SQL Server would reject that, so always declare the type you mean. It documents the table and keeps your SQL portable.
INTEGER PRIMARY KEY
A primary key identifies each row, so no two rows can share it. In SQLite the standard choice is id INTEGER PRIMARY KEY, and it comes with a bonus: insert a row without an id and SQLite numbers it for you (1, 2, 3 …), just as you saw with INSERT. Every id in the course database works this way.
INSERT INTO taverns (name, city, rooms, ale_price)
VALUES ('The Prancing Griffin', 'Eldermoor', 12, 2.5); -- gets id 1
Looking at a table's structure
To list the columns of any table, ask SQLite:
SELECT name, type, pk FROM pragma_table_info('taverns');
You get one row per column with its declared type, and pk = 1 marks the primary key. Try it on heroes too.
Common mistakes
- A comma after the last column.
ale_price REAL,followed by)is a syntax error. - A missing comma between columns.
name TEXT city TEXTdoesn't fail. It creates one column callednamewith the strange typeTEXT city TEXT. INT PRIMARY KEYinstead ofINTEGER PRIMARY KEY. Only the exact wordINTEGERturns on automatic numbering in SQLite. WithINT, rows inserted without an id getNULLas their id.- Creating a table that already exists fails with
table taverns already exists.CREATE TABLE IF NOT EXISTSskips the statement instead.
Assignment
-
Create a table
spellswith these columns, in this order:Column Type Notes idINTEGERprimary key nameTEXTschoolTEXTmana_costINTEGERcast_timeREALin seconds -
Insert these three spells without giving an id, so they get ids 1, 2 and 3:
name school mana_cost cast_time 'Fireball''fire'301.5'Frost Nova''frost'250.5'Mend''healing'152.5
Premium lesson
Unlock the full Learn SQL course
- All 30 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .