Why applications use a database
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A database is an organised collection of data that a program called a database management system looks after for you. Almost every app you use, from train booking to online shopping, keeps its data this way instead of in plain files.
Storing data in files, and where it breaks
A lost update happens when two people change the same data at the same time and one person's save overwrites the other's change. Nothing reports an error, so the data is simply wrong.
A file is the simplest place to keep data. A CSV file (comma-separated values) such as fees.csv stores one record per line, with commas between the values. For one person and a few hundred lines, this works well.
Example 1 · Two clerks, one file: a lost update
fees.csv at 10:00roll,name,paid21,Asha,NO22,Ravi,NO10:01 Clerk A opens fees.csv and marks Asha as paid.10:02 Clerk B opens fees.csv and adds Meena, roll 23.10:05 Clerk A saves: Asha is now YES.10:06 Clerk B saves the copy that B opened at 10:02.fees.csv at 10:07 roll,name,paid 21,Asha,NO 22,Ravi,NO 23,Meena,NO
What this code does
- Clerk B opened the file before Asha's payment was saved, so B's save wrote the old line back over A's change. This is called a lost update.
- Nobody saw an error. The file simply holds wrong data, and Asha may get a reminder for fees she has already paid.
Problems start when the data grows and many people use it at the same time. Two clerks edit the same file and one person's change vanishes. The power fails halfway through a save and the file is left half-written. Nothing stops someone typing abc in the fees column or using the same roll number twice.
Searching also gets slow. To find one student among 50,000 lines, a program reads the file from the top until it finds the match, every single time.
What a database management system (DBMS) does
A database management system (DBMS) is software that stores data on disk and is the only program that reads and writes it. Apps send it requests, usually in SQL, and it checks, orders and saves every change.
A database management system (DBMS) is software that stores data on disk and is the only program allowed to touch it. Apps never open the data files themselves.
Example 2 · Ask for what you want, get a table back
SELECT name, cityFROM customersWHERE city = 'Chennai'ORDER BY name;+------------+---------+ | name | city | +------------+---------+ | Meena Iyer | Chennai | | Ravi Kumar | Chennai | +------------+---------+
What this code does
- This query runs on the course's practice store. It asks for the name and city of every customer in Chennai, sorted by name. The setup lessons in this module get MySQL running so you can try it yourself.
- The query never says which file to open or how to search it. Finding the rows is the DBMS's job; your job is to say clearly what you want.
Apps ask the DBMS for things such as "give me every customer in Chennai", and the DBMS does the reading and writing.
The requests are written in SQL (Structured Query Language), the language almost every DBMS understands. You say what you want; the DBMS works out how to find it. MySQL, the DBMS this course uses, is one of the most used in the world. Because every change passes through one program, the DBMS can check it, order it against other changes and record it safely. That single gatekeeper is what fixes the file problems.
Many users at once, crash safety and fast search
A transaction is a group of changes that the database saves completely or not at all. If the power fails in the middle, the finished part is undone, so the data is never left half-changed.
A DBMS solves the file problems with a few built-in services. You get all of them the moment your data lives in a database, without writing any extra code.
Example 3 · The database refuses a duplicate email
INSERT INTO customers (name, email, city, joined_on)VALUES ('Asha R', 'asha.rao@example.com', 'Mysuru', '2025-06-01');ERROR 1062 (23000): Duplicate entry 'asha.rao@example.com' for key 'customers.email'
What this code does
- The practice
customerstable has a rule: everyemailmust be unique. Customer 1, Asha Rao, already usesasha.rao@example.com. - The DBMS rejects the new row and says exactly why. A plain file would have saved the duplicate without a word.
Example 4 · A UPI transfer when the power fails
Transfer Rs 500 from Ravi to Meena step 1: Ravi's balance 2000 -> 1500 step 2: Meena's balance 1000 -> 1500The power fails right after step 1.Plain file: Ravi 1500, Meena 1000 (Rs 500 has vanished) Database: Ravi 2000, Meena 1000 (step 1 undone on restart)
What this code does
- With a file, step 1 is already on disk when the power fails, so the money simply disappears.
- A DBMS runs both steps as one transaction. On restart it finds the unfinished transaction and undoes it, so either both steps happen or neither does.
The rules are the easiest to see. When a table says every email must be different, the DBMS refuses a second customer with the same email, whichever app or person sends it.
- Many users at once: thousands of people read and change data together, and the DBMS orders their changes so none is lost.
- Crash safety: a group of changes, such as the debit and credit of a UPI transfer, is saved completely or not at all, even if the power fails. Such a group is a transaction.
- Fast search: an index, like the index at the back of a textbook, lets the DBMS jump to matching rows instead of reading everything.
- Rules and security: the DBMS rejects bad data (a duplicate email, text in a number column) and lets each user see only what they are allowed to.
Predict the output
Predict the result
Customer 2 already has the email ravi.kumar@example.com. What does the database do?
Where the database sits in apps like IRCTC, an online store and a college ERP
An application server is a program on the company's computers that sits between the app and the database. It checks each request, sends SQL to the database and builds the answer the app shows.
Your phone never talks to the database directly. A typical app has three parts: the app on your phone or browser, an application server (a program on the company's computers) and a database server (the computer running the DBMS).
Example 5 · One train search, step by step
[ IRCTC app ] -> [ application server ] -> [ database server ] on phone checks the request runs the SQL query writes the SQL returns matching rows1. You search: Chennai to Bengaluru, 12 June. 2. The app sends the search to the application server. 3. The server sends a SELECT query to the database. 4. The database finds the trains and free seats and returns rows. 5. The server builds the results page and the app shows it.
What this code does
- Only the application server talks to the database. The phone never holds the database password, which keeps the data safe.
- The application server can be written in any programming language. Every language sends the same kind of SQL to the database.
A server is a computer that runs all the time and answers requests.
When you search for trains, the IRCTC app sends your search to the application server, which asks the database. The rows come back, the server builds a results screen, and your phone shows it. The database is the one place where the truth lives: which seats are booked, which online order is out for delivery, which marks the college ERP holds for you. An ERP (Enterprise Resource Planning system) is the software a college uses for admissions, fees, attendance and results.
Order of execution
One train search, in order
Put the steps of a train search in the order they happen.
Next step
Check your understanding
10 quick questions