Basic Example

This example shows how to create a users table:

CREATE TABLE users (
    id INTEGER,
    email TEXT
);

The resulting table has two columns:

  • id
  • email

How to Create a Table with a Primary Key

This example makes the id column the primary key of the table:

CREATE TABLE users (
    id INTEGER PRIMARY KEY NOT NULL,
    email TEXT
);

The resulting table has two columns:

  • id
  • email

How to Create a Non-Nullable Columns

You can create non-nullable columns by adding a NOT NULL constraint:

CREATE TABLE users (
   id INTEGER PRIMARY KEY NOT NULL,
   email TEXT NOT NULL
);

How to Create Columns With Default Values

CREATE TABLE users (
   id INTEGER PRIMARY KEY NOT NULL,
   email TEXT NOT NULL,
   credits_used INTEGER DEFAULT 0,
   subscribed_to_newsletter BOOLEAN NOT NULL DEFAULT false
);

Here the following columns specify default values:

  • credits_used with default value 0
  • subscribed_to_newsletter with default value false. This column is furthermore defined to be non-nullable

Learn More

Get started with DB Pilot

DB Pilot is a Database GUI client and SQL editor for PostgreSQL, MySQL, SQLite, DuckDB & more.