Appendix B. An Incredibly Brief Introduction to Relational Databases

“I thought the whole point of Rails was that it hid the database and just let me write Ruby code! Why do I need to know about these things?”

Rails has all kinds of features for building web applications, but its foundation component is the way that it lets you get information into and out of relational databases. You can build simple applications without knowing much about databases, just telling Rake to do a few things and making sure you gave Rails the right data type for each field. You don’t need to know Structured Query Language (SQL), the classic language for working with databases.

Building a more complex Rails application, though, really demands at least a basic understanding of how relational databases work. It helps to think about tables and their connections when defining Rails models, at least when you first set them up.

Tables of Data

The foundational idea underneath relational databases is a simple but powerful structure. Each table is a set of sets, and within a single table all of these sets have the same data structure, containing a list of named fields and their values. For convenience, each set within a table is called a row, and each field within that row is part of a larger named column, as shown in Figure B-1. It looks a lot like a spreadsheet with named columns and unnamed rows.

The classic row–column approach to tables

Figure B-1. The ...

Get Learning Rails: Live Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.