Databases are a mission-critical part of any company's resources. If you program in Perl, you'll want to learn about the DBI, which can help you connect to many popular databases. This article, the first part of a series, is excerpted from chapter 15 of the book Beginning Perl (Apress; ISBN: 159059391X).
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 keyplayer_idand 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 itmusicians) with three fields (player_id,name, andphone) and six rows of information. With this one example we have defined most of our relational database buzzwords, except relational.