Skip to content
Codewilds

Lesson 22 of 30 · Designing Tables

SQL CREATE TABLE: Columns, Data Types and Primary Keys

easy10 XP2 min read

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 TEXT doesn't fail. It creates one column called name with the strange type TEXT city TEXT.
  • INT PRIMARY KEY instead of INTEGER PRIMARY KEY. Only the exact word INTEGER turns on automatic numbering in SQLite. With INT, rows inserted without an id get NULL as their id.
  • Creating a table that already exists fails with table taverns already exists. CREATE TABLE IF NOT EXISTS skips the statement instead.

Assignment

  1. Create a table spells with these columns, in this order:

    Column Type Notes
    id INTEGER primary key
    name TEXT
    school TEXT
    mana_cost INTEGER
    cast_time REAL in seconds
  2. Insert these three spells without giving an id, so they get ids 1, 2 and 3:

    name school mana_cost cast_time
    'Fireball' 'fire' 30 1.5
    'Frost Nova' 'frost' 25 0.5
    'Mend' 'healing' 15 2.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
Play the 7 free lessons

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 .

Write code ↓
SQL+10 XP

Lesson 22 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.