-1

In my app, I am making a call to ebay API and save their categories to my local table.

The API response is a list of categories.

  1. Each category could have more than one parent.
  2. Each parent category could have more than one child category.
  3. There could be up to 6 sub categories

How should the table structure look like? I am using MySQL and Hibernate.

The tree approach of id, name, parent_id cannot work as id could have more than one parent_id

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

0

you could use another table where you store only the parent-child relationships

category          children
-----------       ------------
id INT            category_id INT
name VARCHAR      child_id INT
...

example

as a tree:

                       cat1
                     /   |  \
                cat2 – cat3   cat4
                         | \
                       cat5  cat6

in words:

cat1 has children: cat2, cat3, cat4
cat2 has no children
cat3 has children: cat2, cat5, cat6
cat4 has no children
cat5 has no children
cat6 has no children

In your tables it would looke like this:

category          children
------------      ----------
1, "cat1"         1, 2
2, "cat2"         1, 3
3, "cat3"         1, 4
4, "cat4"         3, 5
5, "cat5"         3, 6
6, "cat6"         3, 2

So in your additional table children you don't have a unique id but you can have each category_id multiple times, each time relating it to another child

[EDIT]
I added 2 to be the child of 3 and 1 (not that obvious in the tree). The children ids and the parent ids can occur multiple times in the children table. They are not unique.

[EDIT2]

This is a SQL query to get one parent and all its children (first generation):

SELECT id, name 
  FROM category 
  WHERE id=1 
UNION 
 (
  SELECT children.child_id, category.name   
     FROM children 
  JOIN category 
     ON children.child_id=category.id 
  WHERE children.category_id=1
 )

Haven't tested it, but something like this should do the trick.
It will return this list:

1 "cat1" 2 "cat2" 3 "cat3" 4 "cat4"

which you can format like this:

 1 "cat1"      <-- parent   
 |          
 +– 2 "cat2"     <-- the children
 +– 3 "cat3"
 +– 4 "cat4" 

Now you will have to play with JOIN and UNION a little bit or simply call this statement in a nested loop for every parent and every child generation.

MySQL is holding 2D- tables with rows and columns, so to have more dimensions (like more generations) you have to put the data together either with the JOIN / UNION statements or outside the DB in your program.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • How does it support multiple parents? – Dejell Feb 24 '14 at 18:31
  • @Dejel: you can have multiple parents the same way. For example add 3,2 to the children table. that way 2 is the child of 1 and 2 is the child of 3 – GameDroids Feb 24 '14 at 18:41
  • So actually children table is: category_id, child_id – Dejell Feb 24 '14 at 19:23
  • oops, you are right :) sorry, yes it's actually category_id, child_id – I changed it – GameDroids Feb 24 '14 at 19:56
  • The question is now, in case that I have a parent category, how do I retrieve all the kids like a tree? – Dejell Feb 24 '14 at 20:46
  • `SELECT id, name FROM category WHERE id=1 UNION (SELECT children.child_id, category.name FROM children JOIN category on children.child_id=category.id WHERE children.category_id=1)` this is just a guess. If I am not wrong, the first `SELECT` gets you one parent category (id and name) and the `UNION` will merge it with a list of children (each of the children are `JOINED` with the category table to get the name of the category. You will have to adjust the query, though to retrieve the columns you want to have – GameDroids Feb 24 '14 at 21:04
  • so do you mean that I will have to do multiple join to get to a lower level? – Dejell Feb 24 '14 at 21:21
  • The above will return the first level I could have up to 6! – Dejell Feb 25 '14 at 15:33
0

Take a look attached image for relations, You needs to extend according to your need. Hope it will help you to design your schema. enter image description here

Suzon
  • 749
  • 1
  • 8
  • 21
  • why do the sub_category and category tables are different – Dejell Feb 24 '14 at 19:09
  • @Dejel: To keep Profile of sub cat items... it may vary with category – Suzon Feb 24 '14 at 19:13
  • and what is the advantage of separating category to sub category and parent to child category? how would the retrieve SQL look like? – Dejell Feb 24 '14 at 19:17
  • If there is any profile change then we could separate CAT and SUB_CAT otherwise no. parent and child will store the IDENTIFIER only to get parent and child. Data retrieving statement will be vary depending on your CAT & SUB_CAT by reference – Suzon Feb 24 '14 at 19:26