liveBook · Manning

Data integrity

As explained in Just Use Postgres!

Data integrity is the correctness, consistency, and validity of data throughout its storage and modification.

Data integrity preserves the accuracy, consistency, and completeness of data throughout its lifecycle. In PostgreSQL, transactions and database-level rules help prevent application changes from leaving inconsistent records. section 2.3

PostgreSQL enforces integrity through complementary defenses:

  • Data types reject values with incompatible formats, such as text in a NUMERIC price column. section 2.3.1
  • NOT NULL requires a value; UNIQUE prevents duplicates; PRIMARY KEY combines uniqueness and non-nullability; FOREIGN KEY links values to another table. section 2.3.1
  • CHECK constraints validate custom conditions, such as allowing only known product categories or nonnegative stock. section 2.1.3 section 2.3.1

Rejecting an invalid product price

Add a database rule, then test it: ALTER TABLE products.catalog ADD CONSTRAINT catalog_price_check CHECK (price > 0); UPDATE products.catalog SET price = -16 WHERE name = 'Morning Glory'; PostgreSQL rejects the update with a check-constraint violation instead of storing the negative price. Similar rules can require review text and restrict review ranks to values between 1 and 5.

How Just Use Postgres! covers it

How deep it goes
Explains data types and six constraint categories, then demonstrates adding and testing checks; it stops short of deeper transactional internals.
Examples
Demonstrates product catalogs, product reviews, category and stock checks, positive prices, required review text, and review ranks from one to five.
What it assumes
Assumes basic relational databases and SQL, while demonstrating standard PostgreSQL capabilities through concrete schemas and commands.
What you can do after
Apply it via PostgreSQL constraints to reject invalid prices, missing reviews, unsupported categories, and out-of-range values.
How it connects
Connects data types, constraints, transactions, relational databases, and application-level validation.

Also explained in

Compare how 18 books explain data integrity →

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.