I am building a classified-ads website using asp.net web forms and linq-to-sql.
The items for sale need to be categorised in a hierarchical manner, however, an individual item may be in multiple child categories.
For an example take a look at this battery on eBay. Notice there are two breadcrumbs:
Sound & Vision > Multipurpose Batteries & Power > Rechargeable Batteries
and well as:
Computers/Tablets & Networking > Laptop & Desktop Accessories > Laptop Batteries
In addition to an item belonging to multiple categories, a category may have more than one parent as well, e.g. when browsing the categories “Sound & Vision > Batteries” or “Electronics > Batteries”, one should see the exact same sub-categories of Batteries in both cases (e.g. Rechargeable or Non-Rechargeable, etc).
I’m uncertain as to how to even begin structuring the database tables, let alone querying them from the web site, so any help or guidance would be much appriciated.
I’ve looked at articles such as Storing Hierarchical Data in a Database, but don’t think it would apply in my case due to the many-to-many nature of the items and categories.
Thanks.
Thanks to @bhrugesh-patel and @andrew-walters for attempting to answer my question. However, on further research it appears that the hierarchy described above is called a Directed Acyclic Graph (DAG), i.e. almost a tree but with one major difference: you can reach the same node through different paths.
Graph Databases such as Neo4j are designed for storing structures like DAGs, but as I’m stuck with using a relational database (SQL Server), I’ll attempt to implement the solution mentioned in this article: A Model to Represent Directed Acyclic Graphs (DAG) on SQL Databases.
FYI: Another question on SO also discusses this topic in more detail.