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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
- SourceClear Open
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide