Designing Databases

Structuring tables can improve database performance-- here's how to do it.
Handling Rail Lines

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:

   name VARCHAR(50) NOT NULL,

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 (
   north_to_south TINYINT UNSIGNED NOT NULL,
   UNIQUE(station_id, line_id),
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)
   (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)
   (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:

FROM RailStations S, StationLines L
WHERE L.line_id = 2
  AND = 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 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:

FROM  RailLines L, StationLines SL
WHERE SL.station_id = 4
  AND = 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:
FROM RailLines L, StationLines SL, RailStations S
WHERE = SL.line_id
  AND SL.station_id =
  AND = "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
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.