Jump to content
Larry Ullman's Book Forums

Recommended Posts

category (id, name)

1 / Category 1

2 / Sub 2

3 / Category 3

 

three (id, parent*)

1 / 0

2 / 1

3 / 0

 

Result:

Category 1

---> Sub 2

Category 3

-------------------

Underlined: Primary key

Asterix: Foreign key

I did not quite understand this.

How about this

First table

Category

Cat_id Parent_id Item

 

Subcategory

ID Sub_id Item

 

the sub_id in subcategory is the parent id in category

Link to comment
Share on other sites

I would assume that a category would contain mulitple subcategories. That means that there is a one-to-many relationship between the category and subcategory tables. In which case, the category ID should be a foreign key in the subcategories table.

 

For example:

 

Category table

id   category_name
1    cars
2    trucks

---

Subcategory table
id   category_id (foreign key)   subcategory_name
1    2                           Ford trucks
2    1                           Ferrari sports cars
3    1                           Honda coupes

 

Anything, I think you get the point. Then, you could use joins to get the proper info and have a properly normalized DB.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...