An extension of A Precis of SQL but now with specific regard to more realistic backend usage.

General

Create

CREATE object_type object_name [... options];

Object Types

Object TypeMeaningExample
TABLE PostgreSQLCreates a new database tableCREATE TABLE users (id SERIAL, name TEXT);
TYPE PostgreSQLDefines a custom enum or composite typeCREATE TYPE mood AS ENUM ('happy', 'sad');
INDEX PostgreSQLCreates an index to speed queriesCREATE INDEX idx_users_name ON users(name);
POLICY PostgreSQLRow-level security policyCREATE POLICY user_policy ON users FOR SELECT USING (id = auth.uid());
TRIGGERExecutes code automatically on database eventsCREATE TRIGGER update_timestamp BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION update_modified_column();
DATABASECreates a new databaseCREATE DATABASE my_database;
VIEWCreates a virtual table (a query stored as a table)CREATE VIEW user_names AS SELECT name FROM users;
FUNCTIONDefines reusable functionsCREATE FUNCTION add_nums(a int, b int) RETURNS int AS $$ SELECT a+b; $$ LANGUAGE SQL;
SCHEMADefines a namespace to organize tablesCREATE SCHEMA analytics;
EXTENSIONInstalls an external PostgreSQL extensionCREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Table

Usage:

CREATE TABLE table_name (
  column1 datatype constraints,
  column2 datatype constraints,
  ...
);

Index

An index is a data structure (usually B-tree) allowing for quick row selection without scanning every row. Great if a column is frequently queried, if the table is large, or column is used for foreign keys. Good examples are email, username, or user_id.

Usage:

CREATE INDEX index_name ON table_name(column_name);

Type

Similar to C (and others), allows the definition of a custom data type beyond the primitive types. They are either 1) enum types or 2) composite types (like a struct).

Usage:

CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
CREATE TYPE address AS (
  street TEXT,
  city TEXT,
  zipcode INTEGER
);

Policy

TODO

Constraints

Rules defined on columns or table to enforce certain data specifications

ConstraintMeaning
PRIMARY KEYEnsures a unique identifier for each row.id SERIAL PRIMARY KEY
FOREIGN KEYEnsures relational integrity (links rows across tables).user_id INTEGER REFERENCES users(id)
UNIQUEEnsures all column values are distinct.email TEXT UNIQUE
NOT NULLEnsures a column always has a value (never empty).price NUMERIC NOT NULL
CHECKEnsures column values meet certain conditions.age INT CHECK (age >= 18)
DEFAULTAssigns a default value if none provided during insertion.status TEXT DEFAULT 'pending'
Example:
CREATE TABLE customers (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  username TEXT UNIQUE NOT NULL,
  email TEXT UNIQUE NOT NULL,
  age INTEGER CHECK (age > 0),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Primary Key

Identifies each row uniquely, cannot have NULL values, and automatically indexed. Great for identifying a specific row and guaranteeing uniqueness, for example employee_id. A table cannot have multiple primary keys.

Foreign Key

Simply put: A foreign key is a column that refers to a primary key in some other table. In other words, a relationship between tables, ensures values in one table match values in the corresponding table.

Unique

Each value in a column must be distinct, with exception to NULL values. This automatically creates an index.

Not Null

Disallows NULL values in a column, meaning a column must always have data.

Check

Validates column data against specified criteria.

Default

Automatically assigns a value if none is provided.