liveBook · Manning

Alter table command

As explained in Just Use Postgres!

The ALTER TABLE command is an SQL statement that changes the definition of an existing table, including its constraints.

ALTER TABLE changes the definition of an existing table, even when it already contains data. It is useful for adding database-level checks when application validation alone may fail. section 2.3.1

Add a positive-price constraint

ALTER TABLE products.catalog ADD CONSTRAINT catalog_price_check CHECK (price > 0); Afterward, updating a product to a negative price fails with a check-constraint error. If existing rows already violate the rule, adding the constraint fails; correct those rows before enforcing it. section 2.3.1

How Just Use Postgres! covers it

How deep it goes
Demonstrates adding checks and NOT NULL requirements, including existing-row failures, but does not detail broader table alterations.
Examples
Uses catalog price validation, a rejected negative-price update, and review NOT NULL and ranking constraints.
What it assumes
Assumes basic SQL tables and constraints, while showing that constraints can be added later when requirements become clear.
What you can do after
Operate/configure it to enforce new table constraints and diagnose failures caused by violating existing or incoming rows.
How it connects
Connects constraints, data integrity, application-level validation, table creation, and psql schema inspection.

Also explained in

Compare how 2 books explain alter table command →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.