October 25, 2016
SQL databases are like airstrips
At a very basic level, SQL databases are tables with an x and y axis that bear relationship to one another. Each column represents a property and each row represents an entry.
Remarkably though, both ‘entry’ and ‘property’ are symbiotes: the properties don’t have anything to apply or attach to without an entry; entries, on the other hand, cannot be described without properties:
We then use SQL, the structured query language, to retrieve sets of data from these relational databases:
Retrieves the name
column from the theodinproject
table:
On a single table, multiple columns are easy to get:
These examples are especially easy to grasp because we are gathering data on all entries and the table returned by SQL retains almost full semblance of the original table. The difficult part then, is to visualise this table as a fluid container where parts change readily like a Transformer would.
For example the following SQL command,
would produce a table only including with both the name
and skills
column but only entries for skills that starting with ‘Ru’:
Now although there are only two axes at work to represent the data here, the columns and rows have rearranged themselves from the initial table to match the query. How you interpret this table shuffling will determine how you visualuse more complex SQL commands.
The analogy I use to best visualise this table shuffling is to think of the database as an airstrip:
All the properties/columns are represented as airstrip lights:
While the entries/rows are represented by plane landings (don’t ask me why planes land tangentially to the airstrip lights… I’m trying my best to describe a SQL table):
When I call:
I set up the name
and skills
airstrip lights. They go on for as far as the eye can see:
Then when I call:
I know that I am retrieving data from theodinproject
airspace:
Then when I call the final part of the query:
This sets up the number of possible landings on our airstrip. Each landing corresponds to one entry:
Which is really just a table:
This analogy is really useful for me because it conveys how Ruby objects can be stored as persistent data. Those property columns are actually mirroring attributes declared with attr_reader :id, :name, :skills
! And then each row entry conveys an instance of the class TheOdinProject
:
Wonderful. Now everything in SQL is an object too.