← SQL and databases

Creating a table

Columns, types and a primary key.

Start this lesson

CREATE TABLE makes a new, empty table. You list each column with a type: INTEGER for whole numbers, REAL for decimals, TEXT for words and dates. One column, usually id, is the PRIMARY KEY: a value that is different for every row, so each row can be found exactly. NOT NULL says a column must always have a value.

CREATE TABLE events (
  id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  event_date TEXT,
  seats INTEGER
);

A table for library events. Every event must have a title; the date and number of seats can be left empty.

In SQLite, a column declared INTEGER PRIMARY KEY fills itself in: if you insert a row without an id, SQLite picks the next number.