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.
|Using Django and MongoDB to Build a Blog||Mar 05, 2014|
|What virtualization solution do you use/manage at work?||Mar 04, 2014|
|Our Assignment||Mar 04, 2014|
|March 2014 Issue of Linux Journal: 20 Years of Linux Journal||Mar 03, 2014|
|Have Resume - Will Travel||Feb 28, 2014|
|March 2014 Video Preview||Feb 28, 2014|
- Using Django and MongoDB to Build a Blog
- Our Assignment
- Sign Up to Win a Silicon Mechanics Swag Pack!
- March 2014 Issue of Linux Journal: 20 Years of Linux Journal
- RSS Feeds
- Linux Systems Administrator
- Have Resume - Will Travel
- Senior Perl Developer
- Technical Support Rep
- Zedge, for All Your Annoying Ringtones!
- You have to be careful there
1 week 2 days ago
- Wonder when LJ is going to
1 week 3 days ago
- Puerto Rico Free Software
1 week 4 days ago
1 week 5 days ago
- I hate voice commands
1 week 6 days ago
- usabilty --- AGAIN with this nonsense
1 week 6 days ago
- Don't make excuses
1 week 6 days ago
- Sorry to let you know, but
1 week 6 days ago
- Ridiculous statement. Not a
2 weeks 9 hours ago
2 weeks 13 hours ago