How to create a table

Define columns

A CREATE TABLE statement names each column and gives it a declared type.

CREATE TABLE courses (
  id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  duration_minutes INTEGER NOT NULL,
  active INTEGER NOT NULL DEFAULT 1
);

Add constraints

  • PRIMARY KEY identifies rows
  • NOT NULL requires a value
  • UNIQUE rejects duplicates
  • CHECK validates a condition
  • FOREIGN KEY protects references

Validate domain rules

A check constraint can reject impossible values close to where data is stored.

duration_minutes INTEGER NOT NULL
  CHECK (duration_minutes > 0)

Adapt generated keys

The example is SQLite-friendly. PostgreSQL typically uses GENERATED ... AS IDENTITY; MySQL commonly declares an integer AUTO_INCREMENT.