Home / SQL / Reference / Tables and design

SQL CREATE TABLE

Create a new table.

Syntax

CREATE TABLE name (
  col TYPE constraints,
  …
);

Each column has a type and optional constraints (PRIMARY KEY, NOT NULL, UNIQUE, CHECK, DEFAULT, REFERENCES).

Example

Query (on the shop database used in the SQL course)

CREATE TABLE notes (
  note_id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  stars INTEGER CHECK (stars BETWEEN 1 AND 5) DEFAULT 3
);
INSERT INTO notes (title) VALUES ('SQL is fun');
SELECT * FROM notes;

Result

note_idtitlestars
1SQL is fun3

1 row

Related

Learn it step by step in the free lesson Creating Tables, with exercises that check your queries.