AlgoViz
Databases
Databases · EasyLesson 1 of 14

What is a database?

When a program runs, it keeps things in memory — the player's score, the list of users, the messages. But memory is like your short-term memory: the moment the program stops, all of it is GONE. A database is where a program writes things down so they're still there tomorrow. But it's much more than a notebook: it keeps the data tidy, finds any piece of it in an instant, and lets lots of people read and write at the same time without making a mess.

Why not just save everything to a file?

You could. For a tiny program, saving to a plain file works fine — write the data out, read it back when you start up. People do exactly this all the time. The trouble starts when the program grows. Imagine a file with a million users in it, and you want just the one named Maya. With a plain file you'd have to read the whole thing, top to bottom, looking for her — slow. Now imagine two people editing that file at the very same second: their changes can clobber each other, and you can end up with a half-written, corrupted file. And if the power dies in the middle of a save? You might lose everything. A database is the tool built specifically to make all of those problems go away.

What a database gives you that a file doesn't

A database is a program whose entire job is to store data well. It hands you four superpowers a raw file never had. First, FAST lookups: ask it 'find Maya' and it jumps straight to her instead of scanning everything (that's what indexes do — a later lesson). Second, STRUCTURE: data lives in neat tables with named columns, so it's never a jumbled blob. Third, SAFE sharing: thousands of people can read and write at once and the database keeps them from trampling each other. Fourth, it never half-finishes: either a change fully happens or it doesn't happen at all, even if the power dies — so your data is never left in a broken in-between state.

The database is a separate program you talk to

Here's the mental model that clears up a lot of confusion: the database is usually its OWN program, running separately from your app — often on its own machine entirely. Your app doesn't open a file; it sends the database a message like 'give me the user named Maya' or 'save this new message,' and the database answers. The language those messages are written in is called SQL (the next lessons are all about it). So the picture is: your app on one side, the database on the other, SQL messages flying between them. That separation is exactly what lets many different apps — the website, the phone app, an admin tool — all share the SAME data safely: they all talk to the one database.

Two big families: SQL and NoSQL

There are two broad styles of database, and it's worth knowing the names even now. A relational (or 'SQL') database stores data in tables with strict columns — like a spreadsheet with rules. It's the classic, careful choice, and it's what most of these lessons teach, because the ideas there (tables, keys, transactions) are the foundation everything else builds on. A NoSQL database is a looser, more flexible style — it might store data as free-form documents or simple key-value pairs, trading some of the strict guarantees for the ability to scale to enormous size more easily. We'll meet it properly later. For now: SQL = tidy and strict, NoSQL = flexible and loose, and most real systems pick one based on what they need most.

It's a tradeoff

Option👍 Pro👎 Con
Use a databaseFast lookups, neat structure, safe simultaneous access by many users, and changes that never half-finish. Built for data that matters and grows.More to set up and run than a file — it's a whole separate program with its own language (SQL) to learn.
Just save to a plain fileDead simple for tiny programs — no extra software, no SQL, just read and write.Slow to search as it grows, breaks when two people write at once, and can corrupt or lose data if a save is interrupted.

Questions you might have

Is a database just a really big spreadsheet?

A relational database LOOKS like a set of spreadsheets — tables with rows and columns — and that's a great starting picture. The difference is everything around it: it can hold billions of rows, find any one in an instant, enforce rules ('every order must belong to a real customer'), and let thousands of people use it at once safely. A spreadsheet falls over at all of those.

Where does the data actually live — in my app?

Usually no. The database is typically its own separate program, often on its own machine. Your app sends it messages ('save this', 'find that') and the database stores the data on its disk. That's why the data survives even if your app crashes and restarts — it was never living inside your app.

What's SQL, exactly?

SQL is the language your app uses to talk to a relational database — the words for 'give me these rows', 'add this row', 'change that row'. You'll learn how to THINK in it in the next two lessons. The key idea: you describe WHAT you want, and the database figures out how to get it.

Why does it matter that a change 'never half-finishes'?

Picture moving $100 from your account to a friend's: subtract from yours, add to theirs. If the power died right after the subtract, your money would vanish into thin air. A database guarantees both steps happen together or neither does — so you can never end up in that broken in-between. This all-or-nothing promise is called a transaction, and it's a whole lesson later.

Do I need a database for a small project?

Not always! For a tiny tool with little data and one user, a plain file is perfectly fine and simpler. You reach for a database when the data grows large, needs to be searched quickly, must stay correct, or is used by many people at once — which is most real apps, eventually.

🧠A database is a separate program whose whole job is storing data WELL: it keeps data in tidy tables, finds any piece instantly, lets many people use it at once safely, and never leaves a change half-done. You talk to it in a language called SQL. Reach for one the moment your data grows, needs fast search, or must stay correct.
✅ Check yourself4 quick questions — prove the idea stuck.Start →