Sub topic 1
What a Database Is and Why Applications Need One
Prerequisites
This chapter assumes you know how an application stores information for later use and can read simple tabular data, such as a spreadsheet. You do not need prior SQL or database experience.
A database is an organized collection of data that an application can store, find, change, and delete. For example, a shopping application may need to remember customer accounts, products, orders, and payments. A database keeps this information after the application stops running.
Without a database, an application could keep data only in its working memory. Working memory is fast, but it is temporary. When the process stops or the computer restarts, that data disappears. An application could also write its data directly to files, but coordinating many users, validating changes, and finding related information becomes difficult as the application grows.
A database provides a structured place for long-lived application data. Database software also manages access to that data. It can let several requests read or change information while protecting the data from conflicting operations.
The application and its data
An application usually has two kinds of state:
- Temporary state is needed while a request or process is running. A search form's current text is an example.
- Persistent state remains available after the process ends. A user's account, saved address, or completed order is persistent state.
The database is responsible for persistent state. The application decides what the data means and when to use it. For example, an application may ask the database for all unpaid orders, calculate shipping costs, and then save the result.
This separation gives each part a clear responsibility:
- The application handles screens, business rules, and user requests.
- The database stores data and provides controlled access to it.
- The database system helps enforce rules, such as requiring every order to belong to a valid customer.
A database is more than a storage box. It helps preserve useful properties of the data:
- Durability means committed data should survive a process failure or restart.
- Consistency means data continues to obey its defined rules.
- Concurrency control coordinates simultaneous operations from different users or requests.
- Querying lets the application retrieve specific data without reading everything.
Why one shared source matters
Suppose an order is stored in one file and the customer's address is stored in another. If the address changes, every copy of that address must be updated. A missed copy creates contradictory information.
A shared database reduces this problem by giving the application one authoritative place for each piece of data. The application can then connect related information when it needs it. This approach also makes backups, access control, monitoring, and recovery easier to manage.
The database does not solve every problem automatically. Poorly designed data, incorrect application rules, or missing backups can still cause failures. A database is a tool for managing data safely, not a substitute for careful design.
A simple working model
When an application handles a request, the usual sequence is:
- The application receives an action, such as “create an order.”
- It checks the request and applies its business rules.
- It asks the database to read or change data.
- The database accepts or rejects the operation.
- The application returns a result to the user.
In a relational database, the stored data is commonly organized into tables. Each table contains related records, and shared values connect records across tables. Later chapters will examine that model, its rules, and the language used to work with it.
Exercise
Why should an application store a completed order in a database instead of keeping it only in memory?
Show answer
Memory is temporary, so the order could disappear when the process stops. A database provides persistent storage and can protect the order while multiple requests access the data.