📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials CREATE TABLE

CREATE TABLE

6 min read Quiz at the end
Create tables with column types and constraints like NOT NULL, UNIQUE, CHECK, and DEFAULT. Use GENERATED ALWAYS AS IDENTITY for auto-incrementing primary keys. Constraints enforce data rules at the database level.

Creating Tables

CREATE TABLE users (
  id        SERIAL PRIMARY KEY,
  name      VARCHAR(100) NOT NULL,
  email     TEXT UNIQUE NOT NULL,
  age       INTEGER CHECK (age >= 0),
  created   TIMESTAMP DEFAULT NOW()
);

Key constraints

  • PRIMARY KEY — unique identifier
  • NOT NULL — value required
  • UNIQUE — no duplicates
  • CHECK — custom validation
  • DEFAULT — auto value
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which constraint ensures no duplicate values?
💡 The UNIQUE constraint ensures all values in a column are distinct.
2. What does NOT NULL mean?
💡 NOT NULL means a value must be provided — NULL is not allowed.
3. Which constraint links two tables?
💡 A FOREIGN KEY references the primary key of another table.
4. What does CHECK do?
💡 CHECK enforces a custom validation condition on column values.
5. What does DEFAULT do?
💡 DEFAULT sets an automatic value when no value is inserted.