Relational Guarantees & Schema Design
Understanding ACID properties, the relational model, and the strict discipline of database normalization.
Why Databases Exist
A naive approach to saving application state is writing a JSON object to a text file. This works perfectly until two separate web requests attempt to modify the same file at the exact same millisecond, corrupting the file permanently.
Databases exist to solve the chaotic reality of concurrent access and hardware failure. A relational database management system (RDBMS) like PostgreSQL or MySQL acts as a rigorous gatekeeper, ensuring that data transitions from one valid state to another, regardless of power outages, network drops, or competing threads.
The ACID Guarantees
Relational databases provide four non-negotiable guarantees, collectively known as ACID:
- Atomicity: A transaction (which may contain multiple queries) is an all-or-nothing proposition. If an engineer attempts to deduct funds from Account A and add them to Account B, and the server crashes mid-way, the database rolls back the entire transaction. Partial states are never saved.
- Consistency: The database strictly enforces its schema constraints (e.g., Foreign Keys,
NOT NULL,UNIQUE). A transaction cannot leave the database in an illegal state. - Isolation: Concurrent transactions execute as if they are the only transaction running on the system. We will explore Isolation Levels deeply in Module 4.
- Durability: Once the database returns a
200 OK(COMMIT successful), the data is permanently written to non-volatile storage. If the power cord is pulled a millisecond later, the data survives.
Schema Design & Normalization
The Relational Model, invented by Edgar F. Codd in 1970, is based on mathematical set theory. The goal of schema design is Normalization: organizing data to eliminate redundancy and prevent anomalies during updates, insertions, or deletions.
- First Normal Form (1NF): Every column must contain atomic (indivisible) values. (e.g., Do not store a comma-separated list of tags in a single string column).
- Second Normal Form (2NF): Eliminate redundant data across multiple rows by extracting it into separate tables and linking them with Foreign Keys.
- Third Normal Form (3NF): Ensure every non-key column is strictly dependent on the Primary Key. (e.g., Do not store a
user_id,department_id, anddepartment_namein the same table. Thedepartment_namedepends on thedepartment_id, not theuser_id).
The Foreign Key Contract
The Foreign Key is the physical manifestation of the relational model. It guarantees Referential Integrity.
If ORDER_ITEMS.product_id is a Foreign Key referencing PRODUCTS.id, the database makes it mathematically impossible to delete a product if an order still references it, preventing “orphan records” and silent data corruption.