Databases
A database is an organized collection of data that can be searched, updated, and used to answer questions. A tool library could use one to record which tools it owns, when each was borrowed, and whether it has been returned. The library can then answer “Which tools are out right now?” without reading through old messages or paper sign-out sheets.
Database management software handles the work of storing and retrieving those records. Designing a database begins with deciding which questions the records should answer, who will update them, and what someone needs to know to understand them later.
How a relational database organizes records
A relational database uses tables. A row represents an individual record, such as a tool or a loan. Columns hold details about that record, such as the name of the tool or the date it was borrowed. A primary key gives a row a unique identifier. The library can use a numeric tool ID even when it owns two drills with the same name.[1]
The tools table can store each tool's name once. A separate loans table can record each time a tool goes out. Each loan contains the tool's ID as a foreign key, which links it to a tool that exists in the first table. The relationship makes it possible to keep a history of several loans for the same tool without copying the tool's details into every record.[2]
Try a small database
In a new SQLite database, create two tables and add these fictional records. A return date of NULL represents a loan that is still open in this example.
PRAGMA foreign_keys = ON;
CREATE TABLE tools (
tool_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE loans (
loan_id INTEGER PRIMARY KEY,
tool_id INTEGER NOT NULL REFERENCES tools(tool_id),
borrowed_on TEXT NOT NULL,
returned_on TEXT
);
INSERT INTO tools (tool_id, name) VALUES
(1, 'Cordless drill'),
(2, 'Orbital sander');
INSERT INTO loans (loan_id, tool_id, borrowed_on, returned_on) VALUES
(101, 1, '2025-04-01', '2025-04-03'),
(102, 2, '2025-04-02', '2025-04-05'),
(103, 1, '2025-04-06', NULL);
Run this SQL query. The JOIN matches each loan with its tool using the tool ID. The WHERE clause keeps only open loans.[3]
SELECT tools.name, loans.borrowed_on FROM loans JOIN tools ON tools.tool_id = loans.tool_id WHERE loans.returned_on IS NULL;
The result is one row, for the cordless drill borrowed on April 6, 2025. Try recording its return by setting a return date for loan 103, then run the query again. Add another tool with no loan history and explain why that tool does not appear in the open-loan results.
Choose a database for the work
Some applications use a relational database because they often need to bring facts from different tables together. A document database can store related fields together in a document, such as a catalog item whose attributes vary by product. A key-value store organizes lookups around a key, such as an identifier used to retrieve a saved session. Start with what the application needs to read and change, then look at how each design would handle those operations.[4]
SQLite runs inside an application and can store its database in a local file. A client-server database such as PostgreSQL can serve several applications or users over a network. For a shared system, consider who will be writing at the same time, who can change a record, and who will run and test the backups.[5]
A transaction groups related changes into one operation. If a checkout records a loan and a deposit in separate tables, the application can commit both changes together. If a required step fails, it can roll back the operation so the database does not record only half the checkout.[6]
Keep a clear rule about which records a team needs and how long to keep them. The tool library may need completed loans to see which tools are used most often. It can decide separately how long it needs any information that identifies the borrower. Document that decision alongside the layout of the database so the next person who maintains it can follow the same practice.
Research databases to explore
- Expanded Human Oral Microbiome Database organizes curated information about microorganisms in the human mouth and related sites.
- GenBank is a public archive of nucleotide sequence records maintained by the National Center for Biotechnology Information.
Discussion questions, essay ideas, and learning related AI prompt ideas
- A small organization keeps the same contact information in several spreadsheets. What questions would help it decide whether to combine those records in a database?
- Write a short proposal for keeping the tool library's records. Explain what information it should store, who needs access, and when it should remove old records.
- Ask an AI tool to write a query that lists the tools currently available, including tools that have never been borrowed. Test it with the sample data and a tool with no loans.
References
- ↑ PostgreSQL Global Development Group. "Concepts", PostgreSQL tutorial.
- ↑ SQLite project. "Foreign Key Support", introduction and instructions for enabling foreign keys.
- ↑ PostgreSQL Global Development Group. "Joins Between Tables", PostgreSQL tutorial.
- ↑ MongoDB. "Data Modeling in MongoDB". Redis. "Keys and Values".
- ↑ SQLite project. "Appropriate Uses for SQLite".
- ↑ PostgreSQL Global Development Group. "Transactions", PostgreSQL tutorial.