Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Question on how to create my SQL table


ranma's Avatar
Member
0 0

I am making a website which will categorize articles. The problem is that I want an article to be able to be part of multiple categories.

For example, "tomato" needs to be part of

"foods -> vegetables -> tomato"

and also

"things to throw -> soft -> tomato"

If I had only one category per article, the table design would be easy - a column for categoryID and no problem.

But what about these multiple categories?

I need to be able to list all the articles in a category, so I cannot merely store the categories for an article as an array (unless I use "LIKE" with some delimiters, I guess, but that sounds ugly.)

Ideas? I'm sure someone here has run into this problem before.


ranma's Avatar
Member
0 0

I may have resolved the problem:

I need to create a table that relates article to category and for each association I need a new entry. The primary key for this table will be composite on both the article and category IDs.

Do you think this will work?


AldarHawk's Avatar
The Manager
0 0

You could also store all the areas in the table separated by a delimiter. Then explode the value from the delimiter and you can have multiple categories and keep one cell for this.


chaos_theory's Avatar
Member
0 0

A working, but less elegant method for that would be to make another table for categories entirely, then have each of those categories able to point at the articles you want. You would have to redo your sql search function to look at a different table.