Skip to content
Lesson 9

Databases As Trustworthy Notebooks

Walk into any kiryana shop in Pakistan and you will see, near the gola or the cash drawer, a thick worn register called the khaata. Each page has a customer's name. Each line has a date, an item bought on credit, the am…

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

Walk into any kiryana shop in Pakistan and you will see, near the gola or the cash drawer, a thick worn register called the khaata. Each page has a customer's name. Each line has a date, an item bought on credit, the amount, and a running total. When the customer comes back to settle, the shopkeeper opens the page, runs his finger down the lines, and reads out the total. The khaata is, with no exaggeration, a database. It stores rows of structured information in a known shape, with one column being a unique key, the customer name, that lets you find any row again. Every database course in any university, in any language, is a fancier version of this idea.

A database is not just storage. It is trustworthy storage. Three promises set it apart from a notes file or a spreadsheet. One, it never loses a write that it told you was saved, even if the power goes off mid sentence. Two, it lets two people add to the same khaata at the same instant without one overwriting the other. Three, it answers questions, like find every customer who owes more than five thousand rupees, in milliseconds, even if there are a million customers. A spreadsheet has none of these. A khaata at scale has none of these either. The database is the engineered form of the same idea.

Hands on. Open sqliteonline dot com, free, no install. On the left side it gives you a sample database. Click new database, then write CREATE TABLE customers paren name TEXT, city TEXT, balance INTEGER paren and a semicolon. Press run. You just made an empty khaata. Now write INSERT INTO customers VALUES paren single quote Asif single quote, single quote Lahore single quote, 4500 paren and run. Repeat with two more, one from Karachi, one from Quetta, with different balances. Finally, write SELECT star FROM customers WHERE balance bigger than 4000. The screen shows you exactly the rows that match. You did in three commands what a kiryana wala does in three thousand rupees of paper khaata each year.

Two ideas you will hear endlessly. Primary key, the column that uniquely identifies a row, the way a CNIC identifies a person. Index, a separate small structure the database keeps so that searching by a column is fast. A primary key is, by default, indexed. Without indexes, asking the database to find one customer by name in a million row table is like a shopkeeper flipping every page of the khaata. With an index, it is like jumping straight to the right page. The first time a senior engineer adds an index to a slow query and watches it drop from twelve seconds to thirty milliseconds, she falls in love with databases. The day will come for you too.

Where do real Pakistani products use this. Easypaisa stores every transaction as a row. NADRA stores every CNIC and the family relationship between them. K Electric stores every meter reading. Your school's report card system, if it has one, stores marks and class sections. Behind every screen you tap on, somewhere, a database query was written by a person, often a young one, often paid less than they should be, often working in Karachi or Lahore. Knowing how to write that query, and to write it without dropping a column of customers' balances, makes you part of the small group that runs the country's quietly important systems.

Estimated time: 14 min