Where are your likes kept?
Log in to Instagram on a brand new phone. Your photos, your followers and every post you ever liked are all there, though this phone has never seen you before. Millions of people are posting and liking at the same moment, and none of your data gets mixed up with theirs.
Something is keeping all of it safe and in order. To see why that is hard, try doing the same job with plain files.
See it#
Picture a small bank that keeps every account in a plain file, the way you'd keep notes on a laptop. Here are two ordinary moments going wrong, and then the same two moments done the other way.
Saved at once: one is lost. Taking turns: both count.
First moment: two clerks open the same file at the same time. Both read Ravi's balance, ₹5,000. Each pays out ₹1,000 and saves. The file now says ₹4,000. Ravi took out ₹2,000, but one withdrawal has vanished, because the second save wrote over the first.
Every line, or straight to her.
Second moment: a customer calls, and the clerk looks her up. The file has no idea where she is, so the search reads every line from the top. One lakh lines, read to find one person.
Done the database way, both jobs come out right. The two withdrawals take turns on Ravi's balance, so it ends at ₹3,000. The search goes straight to her row.
Name it#
Both fixes come from keeping the data a different way. A database is an organised collection of data, kept so that many people and apps can read it, change it and search it at the same time without breaking it.
The software that runs a database, and that every app talks to, is a database management system (DBMS). MySQL, PostgreSQL, Oracle and SQL Server are DBMSs, and so is SQLite, which runs inside almost every phone.
How it works#
1. What goes wrong with files#
The bank's files have more problems than those two moments. Here they are side by side with what a DBMS does instead.
| With plain files | With a DBMS |
|---|---|
| The same customer's address sits in the savings file and in the loan file | Each fact is stored once, and everything else points to it |
| Update the address in one file only, and the two copies now disagree | With one copy, there is nothing to disagree with |
| Two clerks writing at once lose a withdrawal | Changes to the same data take turns |
| Finding one customer means reading every line | It finds rows directly, without reading everything |
| Anyone who can open the file can read all of it | Each user gets only the access they're given |
| Nothing stops a balance of minus ₹50,000 being typed in | Rules refuse bad values before they're saved |
The first two rows have names worth knowing. Storing the same fact in two places is redundancy. Two copies that disagree after an update is data inconsistency, and redundancy is what causes it. The lost withdrawal is a concurrent access anomaly: two changes running at the same moment interfered with each other. How a DBMS makes changes take turns is a later phase (Transactions), and the rules that refuse bad values are the Keys and constraints lesson.
2. The data lives in tables#
Most databases keep data in tables, like a neat spreadsheet. Here is a college's students table:
| roll_no | name | branch | cgpa |
|---|---|---|---|
| 101 | Asha | CSE | 8.7 |
| 102 | Ravi | ECE | 7.9 |
| 103 | Meena | CSE | 9.1 |
Read it two ways:
- A row is one thing. Row 102 holds everything about Ravi: his roll number, his name, his branch and his CGPA. A row is also called a record, or in textbooks a tuple.
- A column is one fact, for every row. The cgpa column holds every student's CGPA, and only that. A column is also called an attribute.
A real database has many tables that point to each other. A marks table says "roll number 102 scored 81 in DBMS" instead of copying Ravi's name again. Keeping data in tables linked this way is the relational model, and it's how the address in the bank gets stored once instead of twice. (How one table points to another is the Keys and constraints lesson, and reading two at once is the Joins lesson.)
3. The design and the data are different things#
Look at the table's header: students(roll_no, name, branch, cgpa). That design is the schema. It says what the table holds, and it rarely changes.
The header row is the schema.
The rows inside it right now are the instance. On Monday the college has 1,200 student rows. On admission day it has 1,350. The instance changed; the schema didn't. Think of a blank admission form and the pile of filled ones: the form is the schema, the pile is the instance.
4. DBMS and RDBMS#
A DBMS is any software that manages a database. An RDBMS (relational DBMS) is one that keeps the data as linked tables, and you talk to it in SQL, the language for asking a database questions. SQL is the next lesson. MySQL, PostgreSQL, Oracle, SQL Server and SQLite are all RDBMSs.
Some databases don't use tables at all. MongoDB stores documents, and Redis stores key and value pairs. They're called NoSQL databases, and they come later in the roadmap. When an interviewer says "DBMS", they almost always mean an RDBMS.
Why it matters#
Almost every app you use stands on a database. Your Swiggy order history, your IRCTC booking, your bank balance, your college results on the ERP portal and the chats you back up are all rows in tables somewhere.
In interviews this comes first, in three forms: "Why use a DBMS instead of a file system?", "What is the difference between DBMS and RDBMS?" and "What is the difference between a schema and an instance?"
What to remember#
- A database keeps data so that many people and apps can use it at once without breaking it. A DBMS is the software that runs it.
- Files fail in three classic ways: redundancy leads to inconsistency, two writers at once lose a change, and every search reads everything.
- A table's rows are things and its columns are facts. One row holds everything about one thing.
- The schema is the design and the instance is the data right now. The schema rarely changes; the instance changes all the time.
- An RDBMS keeps data in linked tables and speaks SQL: MySQL, PostgreSQL, Oracle, SQLite.