The self-join provides the administrator with a powerful method of centralizing relational data to a single table. In fact, the self-join is performed by joining a particular table to itself. Let's illustrate this concept with an example: Suppose we are in control of a large database containing information regarding various pieces of hardware used to build a computer workstation. A workstation may consist of a desk, pc, monitor, keyboard and mouse. Furthermore, the desk can be considered the 'parent' of all other parts of the workstation. We want to keep accurate records of each workstation, so we will correlate all parts of a specific workstation together via a unique id number. Actually, each part will contain two id numbers, one unique to that specific item, and one identifying its' parent (the desk) id number. Assume that this is our table:
Notice that the desktop does not have a parent_id, since it is in fact the parent for all of its' corresponding parts. With the table filled with data, we can now begin querying it for useful information. Also note that while our table is simple for reason of best illustration of use of the self-join, one could provide significantly more useful information regarding each item. So what is the outcome? Like previously seen with such as join regarding two tables, each row from the first table will be matched with every row in the second table. Try it and see. Again, however, this is not very useful to us. Let's look at a more interesting example: We are interested in viewing information regarding a specific workstation in which we several technical support calls had been made. We know what the particular workstation id is (the desk id). Let's query the database to pull up all relevant pieces of this workstation: This provides a much more interesting outcome, displayed as follows:
The self-join is also used as an efficient method of verifying table data. Since the uniqid column within the table is intended to be unique, it would not be good if the data-entry dept. accidentally entered two items with the same uniqid into the database. This could be periodically checked by using a self-join. Assume that we modified the 350 mhz pc uniqid to be 'm156' (which is incidentally the uniqid value of the monitor belonging to workstation 'd002'). Consider the following example: This would result in the following:
There you have it. Table joins made easy. Try playing around with variations of the commands highlighted within this article to gain a clear understanding of the syntax. Once this is understood, you will find that table joins will play an integral part in your development activities. Be sure to check out MySQL's various discussion groups (http://www.mysql.com), as there is usually quite a bit of information exchanged regarding table joins.
blog comments powered by Disqus |