In earlier chapters, we persisted application data using flat files such as plain text, CSV, and JSON. While flat files are adequate for basic storage and configurations, modern software applications require concurrent access, complex relationships, relational integrity, and fast querying across millions of records. This is where Databases become indispensable.
1. Why Flat Files (CSV / JSON) Fall Short
Challenge
Flat File Limitation
Database Solution
Concurrency
If two threads write to a file simultaneously, corruption or race conditions occur.
Built-in locking mechanisms and multi-user concurrency control.
Query Speed
Finding one record requires reading and scanning the entire file into memory ($O(n)$).
B-Tree Indexes allow searching millions of rows in milliseconds ($O(\log n)$).
Data Integrity
Any malformed string can be written, corrupting formats.
Strict schemas, constraints (NOT NULL, UNIQUE), and type validation.
Atomicity
A crash halfway through writing leaves partial, corrupt data.
A Relational Database Management System (RDBMS) organizes data into structured two-dimensional Tables (also called relations).
Table: A collection of related data entries consisting of columns and rows.
Column (Field / Attribute): Defines a specific property (e.g., email, salary, date_of_birth) and its data type.
Row (Record / Tuple): Represents a single, distinct data entity instance.
Primary Key (PK): A unique identifier guaranteeing that every row in the table can be distinguished (e.g. user_id or isbn).
Foreign Key (FK): A column in one table that links directly to the Primary Key of another table, establishing relational associations.
3. SQL: The Universal Language of Databases
SQL (Structured Query Language) is the standardized domain-specific language used to query, define, and mutate relational databases. SQL commands are traditionally categorized into:
1. Data Definition Language (DDL)
Defines the schema and structure of tables:
1CREATETABLE students (
2 student_id INTEGERPRIMARYKEY AUTOINCREMENT,
3 full_name TEXTNOTNULL,
4 email TEXTUNIQUENOTNULL,
5 gpa REALCHECK(gpa >=0.0AND gpa <=4.0),
6 enrolled_date TEXTDEFAULTCURRENT_TIMESTAMP
7);
2. Data Manipulation Language (DML)
Interacts with the rows inside existing tables:
1-- Create (Insert)
2INSERTINTO students (full_name, email, gpa)VALUES('Aarav Sharma','aarav@example.com',3.85);
3
4-- Read (Select)
5SELECT full_name, gpa FROM students WHERE gpa >=3.5;
6
7-- Update
8UPDATE students SET gpa =3.90WHERE email ='aarav@example.com';
9
10-- Delete
11DELETEFROM students WHERE student_id =1;
4. What is SQLite?
SQLite is the most widely deployed SQL database engine in the world, embedded in billions of smartphones, web browsers, operating systems, and IoT devices.
Key advantages of SQLite:
1
Serverless: Unlike client-server databases (like PostgreSQL or MySQL) that require running a background daemon process, opening network ports, and managing user credentials, SQLite reads and writes directly to a regular disk file.
2
Zero Configuration: No installation, configuration, or administrator service management needed.
3
Single Disk File: An entire database—with multiple tables, indexes, triggers, and schemas—is stored in a single cross-platform .db or .sqlite file.
4
Built into Python: Python provides the sqlite3 module in its standard library—ready to use out of the box with zero external dependencies!
5. ACID Properties: The Reliability Guarantee
All production-grade relational databases adhere to ACID guarantees:
A - Atomicity: All operations in a transaction succeed together, or all are rolled back.
C - Consistency: Data must always adhere to defined constraints, rules, and triggers.
I - Isolation: Concurrent transactions execute independently without interfering with each other.
D - Durability: Once a transaction is committed, changes are permanently written to non-volatile disk.
⚠️ Common Intermediate Mistakes & Gotchas
1. Storing Everything in Flat Files
Using flat CSV/JSON files for applications with frequent concurrent writes leads to file lock collisions and data corruption. Relational databases provide ACID transaction guarantees.
2. Missing Primary Keys
Every database table should have an unambiguous primary key (such as an auto-incrementing integer or UUID) to uniquely identify records and facilitate indexing.
💻 Try It Yourself: Database Schema Design
Scenario
Design a SQL CREATE TABLE schema for an e-commerce order management system including primary key, foreign key references, and data validation constraints.