A few words about databases

You can store and retrieve complex information using simple building blocks.

· #substack

black and white plastic drawer

Photo by the blowup on Unsplash

Before I studied computer science, databases were a mysterious and unfamiliar thing to me. After I learned the basic principles, I realized how fundamentally simple they were.

Most of the time when tech people refer to a “database” they mean a “relational database.” This kind of database organizes its stored data in specific ways. Generally, records of data are grouped together in a table. The rows of the table represent different records, while the columns of the table represent the different parts of the record.

For example, if you have a “customers” table, the columns would represent the different things you need to know about a given customer. Typical columns would be their first name, last name, customer number, email address, and so forth. The rows of the table each contain the complete set of information about a different, unique customer.

Visually, you can think of a database table as the cells of a spreadsheet. In fact, many tools for browsing relational databases offer a user interface that looks a lot like a spreadsheet.

The real power of the relational database model comes from the way that tables are connected to each other. For example, suppose that in addition to the “customers” table, we have an “orders” table that contains information about customer orders. Among other columns, we will need a column for a unique order number, as well as the customer number of the customer who placed the order.

The customer number now operates as a connection between the table of customers and the table of orders. (The technical term for the customer number in the order table is “foreign key” - in other words, the customer number is a unique key that identifies a row in a different table.)

Conceptually speaking, we can ask questions like:

Database, please give me the list of all the orders for customer 12345 during the year 2021.

or

Database, please find the customer number for customer “John Smith” and look up all the orders for that customer number during the year 2021.

The first question looks at a single table (customers) while the second question uses two tables (customers and orders).

From these fundamentals, we can build increasingly complicated questions about complicated data sets. As long as we follow the principles of the relational model, everything holds together.

brown wooden drawer

Photo by Jan Antonin Kolar on Unsplash

Originally published on Substack.