Wednesday, June 24, 2015

What is the meaning of One-to-Many Relationships in SQL?


The more than one aspect of the relationship between a row in the categories table and matching rows in the entries table is the fundamental characteristic of what we call a one-to-many relationship. Each (one) category can have multiple (many) entries.
(Parent - Children Relationship)

Even though a given category (blog) might have no matching entries, and only one of the categories (humor) has more than one entry, the relationship between the categories and entries tables is still a one-to-many relationship in structure. Once the tables are fully populated with live data, it’s likely that all categories will have many entries.
(Vendor - Items Relationship)

Looking at this relationship from the other direction, as it were, we can see that each entry can belong to only one category. This is a direct result of the category column in the entries table having only one value, which can match only one category value in the categories table. Yet more than one entry can match the same category, as we saw with the humor entries. 

So a one-to-many relationship is also a many-to-one relationship. It just depends on the direction of the relationship being discussed.

No comments: