Perl and DBI - Introduction to Relational Databases (
Page 2 of 4 )
In order to talk about SQL, we will need to start by talking about relational databases. There are two important facts about relational databases. First, the content in a relational database is persistent—the data continues to exist after the execution of the program that accesses or modifies it. This is much like writing the data to a file on disk that will stay on the disk after the file is created, read from, or modified. The second important fact is that relational databases, unlike files on disk, allow concurrent access and updates from multiple users and processes. This means that more than one user can access the database at the same time—the database server takes care of making sure the changes are made to the data in a safe way.
A relational database, simply put, is a database of tables that can relate to one another in some way. A table is a collection of rows of data. Every row of data has the same basic pieces of information, called fields. There are a lot of buzzwords here, so let’s describe each of these by an example.
Let’s say we want to keep some information about our favorite musicians. The information includes their name, phone number (since we often call them up and chat), and the instruments that they play. We might start by creating a list of the musicians like this:1
| Roger Waters |
555-1212 |
| Geddy Lee |
555-2323 |
| Marshall Mathers III |
555-3434 |
| Thom Yorke |
555-4545 |
| Lenny Kravitz |
555-5656 |
| Mike Diamond |
555-6767 |
This list of musicians shows six lines of data. These lines are called rows in relational database–speak. We would take these six rows and place them together into one collection of data, called a table. Normally, when we place data within a table, we want to create a unique identifier for the row, called a key—just in case we had two different Marshall Mathers III in our table we could access the one we are interested in using this unique value. We will name the key
player_id
and name the other columns, or fields, as well:
| player_id |
name |
phone |
|
1 |
Roger Waters |
555-1212 |
| 2 |
Geddy Lee |
555-2323 |
| 3 |
Marshall Mathers III |
555-3434 |
| 4 |
Thom Yorke |
555-4545 |
| 5 |
Lenny Kravitz |
555-5656 |
| 6 |
Mike Diamond |
555-6767 |
What we have created here is a table (let’s name it
musicians
) with three fields (
player_id
,
name
, and
phone
) and six rows of information. With this one example we have defined most of our relational database buzzwords, except relational.