9 November 2020, 12:15 pm by antelove19
Sources:
Wrox Press Beginning MySQL 2005 ^{1}
Relationships
One of the defining characteristics of a relational database is the fact that various types of relationships exist between tables. These relationships allow the data in the tables to be associated with each other in meaningful ways that help ensure the integrity of normalized data. Because of these relationships, actions in one table cannot adversely affect data in another table.
For any relational database, there are three fundamental types of relationships that can exist between tables: onetoone relationships, onetomany relationships, and manytomany relationships. This section takes a look at each of these relationships.
OnetoOne Relationships
A onetoone relationship can exist between any two tables in which a row in the first table can be related to only one row in the second table and a row in the second table can be related to only one row in the first table. The following example demonstrates how this works. In Figure 48, a onetoone relationship exists between the Authors table and the AuthorsBios table. (The line that connects the tables represents the onetoone relationship that exists between the tables.)
Several different systems are used to represent the relationships between tables, all of which connect the tables with lines that have special notations at the ends of those lines. The examples in this book use a very basic system to represent the relationships.
Figure 4.8
Each table includes a primary key that is defined on the AuthID column. For any one row in the Authors table, there can be only one associated row in the AuthorsBios table, and for any one row in the AuthorsBios table, there can be only one associated row in the Authors table. For example, the Authors table includes a row for the author record that has an AuthID value of 1001 (Edith Wharton). As a result, the AuthorsBios table can contain only one row associated with author 1001. In other words, there can be only one biography for each author.
If you refer again to Figure 48, you’ll see that the AuthorsBios table includes a row that contains an AuthID value of 1004. Because a onetoone relationship exists between the two tables, only one record can exist in the Authors table for author 1004. As a result, only one author can be associated with that author biography.
Generally, onetoone relationships are the least likely type of relationships to be implemented in a relational database; however, there are sometimes reasons to use them. For example, you might want to separate tables simply because one table would contain too much data, or perhaps you would want to separate data into different tables so you could set up one table with a higher level of security. Even so, most databases contain relatively few, if any, onetoone relationships. The most common type of relationship you’re likely to find is the onetomany.
OnetoMany Relationships
As with onetoone relationships, a onetomany relationship can exist between any two tables in your database. A onetomany relationship differs from a onetoone relationship in that a row in the first table can be related to one or more rows in the second table, but a row in the second table can be related to only one row in the first table. Figure 49 illustrates how the onetomany relationship works.
Figure 4.9
As you can see in the figure, there are three tables: Authors, AuthorBook, and Books. Notice that the lines connecting the Authors table and the Books table to the AuthorBook table have three prongs on the AuthorBook side. This is sometimes referred to as a crow’s foot. The three prongs represent the many side of the relationship. What this means is that, for every row in the Authors table, there can be one or more associated rows in the AuthorBook table, and for every row in the Books table, there can be one or more associated rows in the AuthorBook table. For every row in the AuthorBook table, however, there can be only one associated row in the Authors table or the Books table.
Notice that each table includes an identifying column designated as the primary key. In the Authors table, the primary key is the AuthID column, and in the Books table, the primary key is the BookID column. For the AuthorBook table, the primary key is defined on both columns. This is another example of a composite primary key.
If you take a look at the AuthorBook table, notice that the first column is AuthID. The column contains AuthID values from the Authors table. This is how the onetomany relationship is implemented, by referencing the primary key of the one side of the relationship in a column on the many side. The same thing is true for the Books table. The BookID column in the AuthorBook table contains the BookID values from the Books table. For example, the first row in the AuthorBook table contains an AuthID value of 1006 and a BookID value of 14356. This indicates that author 1006 wrote book 14356. If you now refer to the Authors table, notice that author 1006 is Hunter S. Thompson. If you refer to the Books table, you’ll see that book 14356 is Hell’s Angels. If an author has written more than one book, the AuthorBook table contains more than one row for that author. If a book is written by more than one author, the AuthorBook table contains more than one row for that book.
A onetomany relationship is probably the most common type of relationship you’ll see in your databases. (This would also include the manytoone relationship, which is simply a reversing of the order in which the tables are physically represented.) The next section deals with the manytomany relationship.
ManytoMany Relationships
A manytomany relationship can exist between any two tables in which a row in the first table can be related to one or more rows in the second table, but a row in the second table can be related to one or more rows in the first table. Take a look at an example to help illustrate how this relationship works. In Figure 410, you can see three tables: Authors, AuthorBook, and Books. Authors and Books are connected by a dotted line that represents the manytomany relationship. There are three prongs on each end. For any one author in the Authors table, there can be one or more associated books. For any one book in the Books table, there can be one or more authors. For example, author 1009 — Annie Proulx — is the author of books 19264 and 19354 — Postcards and The Shipping News, respectively — and authors 1011 and 1012 — Black Elk and John G. Neihardt, respectively — are the authors of Black Elk Speaks.
Figure 4.10
Also notice in the Authors and Books tables that there is no reference in the Authors table to the books that the authors have written, and there is no reference in the Books table to the authors that have written the books. When a manytomany relationship exists, it is implemented by adding a third table between these two tables that matches the primary key values of one table to the primary key values of the second table. You saw examples of this during the discussion of normalizing data earlier in this chapter. The manytomany relationship is logical and is not physically implemented, which is why a dotted line represents the relationship in Figure 410. In the next section, =="Creating a Data Model"==, you learn how identifying manytomany relationships is part of the database design process.

Wrox Press, "Wrox Press Beginning MySQL 2005", page 116 ↩