Designing Databases
Unlike cars, buses and airplanes, trains run along fixed lines. Each line must have at least two stations, and each station is on one or more lines.
We could have included an additional “lines” column in RailStations, identifying the line with which each train is associated. But given such a table, how would we handle stations that sit on more than one line? It would not make sense to treat the station as two separate stations, particularly if people will need to switch trains.
A better solution uses a separate RailLines table, defined similarly to RailStations:
CREATE TABLE RailLines ( id TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, UNIQUE(name) );
Now that we have a list of lines and stations, we will create a third table that describes the intersection between the two:
CREATE TABLE StationLines ( station_id TINYINT UNSIGNED NOT NULL, line_id TINYINT UNSIGNED NOT NULL, north_to_south TINYINT UNSIGNED NOT NULL, UNIQUE(station_id, line_id), INDEX(station_id), INDEX(line_id), INDEX(north_to_south) );StationLines is a table that brings together the train stations and the lines on which they sit. station_id and line_id contain values from the “id” columns in RailStations and RailLines, respectively. And north_to_south is an integer value that counts the number of stops between the beginning of the line and the named station. Thus, the northernmost station on a rail line would be assigned 1, the next station would be assigned 2, the station after that would be assigned 3, and so forth.
Because each station can sit on more than one rail line, and each rail line contains more than one station, we should not use the UNIQUE modifier with those columns. However, we do not want the combination of any station and line to appear in the table more than once. We enforce this by naming both station_id and line_id as arguments to UNIQUE. Either of those columns may appear multiple times, but the combination of any two values may appear only once.
For example, the following row places station_id 1 as the northernmost station on line_id 1:
INSERT INTO StationLines (station_id, line_id, north_to_south) VALUES (1, 1, 1);
The following indicates that station_id 7 is 11 stops from the beginning of line_id 4:
INSERT INTO StationLines (station_id, line_id, north_to_south) VALUES (7, 4, 11);
With StationLines defined, we can begin to ask the database basic questions. For instance, we can list the stations on line two:
SELECT station_id FROM StationLines WHERE line_id = 2 ORDER BY north_to_south;
This query produces the following result:
+------------+ | station_id | +------------+ | 6 | | 4 | | 5 | +------------+While this answer is indeed correct, it is not very useful. After all, why should I have to remember various stations' ID numbers in order to use the system?
Fortunately, relational databases permit us to join two tables, allowing us to connect the station's ID number with its name. To avoid confusing columns from the two tables, we name each column using the table.column syntax, separating the two with a period. And to reduce the amount of typing we must do, we give each table a nickname.
For example, we can structure our query such that it selects information from both RailStations and StationLines:
SELECT S.name FROM RailStations S, StationLines L WHERE L.line_id = 2 AND S.id = L.station_id ORDER BY north_to_south;
The query now produces the following results:
+-------------------+ | name | +-------------------+ | Lod | | Tel Aviv Central | | Tel Aviv Hashalom | +-------------------+Beginning database programmers often make the mistake of not qualifying their joins enough; that is, not putting enough statements in the WHERE clause. This is because a database server produces a join by combining every row in RailStations with every row in StationLines. The WHERE clause tells the server which rows to remove from the resulting table.
In the example above, the database server first creates a table of 112 rows (8 rows in RailStations x 14 rows in StationLines). It then removes all rows in which L.line_id is not 2, producing 24 rows. It then applies the final criterion, throwing out those rows in which S.id and L.station_id are unequal. The result is three rows.
Because we have broken down the data into three tables and we can join any combination of tables with any set of criteria, our database can already help us answer some basic questions. For example, which rail lines connect to the Tel Aviv Central station? Knowing the ID of that station is 4, I can compose the following query:
SELECT L.name FROM RailLines L, StationLines SL WHERE SL.station_id = 4 AND L.id = SL.line_id;
That query produces the following results:
+-------------------------------+ | name | +-------------------------------+ | Nahariya - Tel Aviv | | Tel Aviv - Be'er Sheva | | Binyamina - Tel Aviv suburban | +-------------------------------+If I join a third table in my query, I can use the station's name, rather than its ID number:
SELECT L.name FROM RailLines L, StationLines SL, RailStations S WHERE L.id = SL.line_id AND SL.station_id = S.id AND S.name = "Tel Aviv Central";You might think the latter query, in which we name the station explicitly, would be the more common and useful when designing database applications for the Web. In fact, it's not. <select> lists and other HTML form elements distinguish between the value passed to the server and the value displayed to the user. For example:
<select name="station"> <option value="4">Tel Aviv Central </select>The above one-element <select> list gives us the best of both worlds—it displays the station name to the user, but actually passes the ID associated with that station. This means our query can join two tables rather than three, which reduces the amount of memory it uses, as well as the speed with which results are returned to the client.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




52 min 45 sec ago
1 hour 8 min ago
3 hours 11 sec ago
8 hours 52 min ago
13 hours 23 min ago
13 hours 24 min ago
15 hours 24 min ago
1 day 9 min ago
1 day 43 min ago
1 day 1 hour ago