As we discussed in the requirements and design section, the database model for our application is fairly simple and contains only two entities: the virtual host definition and the configuration directive definition. For the implementation, however, we also need to add a third element into the schema that ties the virtual host and the configuration directive elements. The reason for adding yet another table is that each configuration directive can be part of one or more virtual hosts. Also, there might be one or more directives in each virtual host. Therefore, we have a many-to-many relationship between the objects, and in order to resolve that we need to insert an intermediate table that has a one-to-many relationship with the other tables.
We can represent this relationship model in the entity relationship (ER) diagram shown in Figure 5-1, where you can see the properties of each entity and the relationships between them. ER diagrams are really helpful when coding and sometimes save you from writing complex code just to find information that can be easily obtained with a simple SQL statement if you know the relations between different tables. We'll use this technique again in later chapters.
ConllgDirecllve id INT
(toe uine nation VARCHARrMO)
ts .container BOOLEAN
PRIMARY directive vhost parent
■Note The diagram in Figure 5-1 was produced using the MySQL Work Bench tool. It follows the convention and structure used to represent the data tables and also the relationships between them (one-to-many links, and so on). The description of those details is beyond the scope of this book, but if you want to learn more about the subject, I recommend Beginning Database Design: From Novice to Professional, by Clare Churcher (Apress, 2007), which is a good introduction to database design. A much shorter description of some of the symbols used in the diagram can be found on the Wikipedia page http://en.wikipedia.org/wiki/Entity-relationship_model.
You can see that the ConfigDirective and the VirtualHost tables have a one-to-many relationship with the VHostDirective table. This table also holds the value for the configuration directive, which is specific to the particular virtual host. You may also have noticed that the VHostDirective has a loop-back relationship to itself. This is to implement the directive encapsulation, where some directives can be the "parent" directives for others.
Was this article helpful?