1

I am developing a web application where users can create the following resources/contents:

Events | Music | Posts | Classifieds

They have alot of fields in common, such as:

created_date | title | desc | user_id

Now I am wondering if I should create separate tables for each content, or save them all in one table, with a type_id foreign key, which points to a content_type table. Ofcourse, some distinct fields will be there which will be only used by specific content types, for those not using those fields, I can just leave it blank.

Data looks more organized with separate tables for each content type, but searching for a keyword across all tables is becoming a nightmare(with joins, unions etc). If it was just a single table, searching will be very easy.

I need that the user be able to search across all content with a keyword. He would also be able to search specific contents, for that I will do a WHERE clause on the type_id field.

I am not aware of all the pros/cons of each method, but I would appreciate if people could advice me so that I don't make the wrong decision, and have to redo everything from start.

4
  • All depends on how you are going to use the data. Apr 2, 2013 at 21:19
  • @nathanhayfield I just can't combine the search results from different tables. If I just do a simple array merge with the search results, then the final results won't be in their proper ranking. They'd be ordered according to the order in which content was first searched by the application. I need the most relevant result mixed from across all contents.
    – WebNovice
    Apr 2, 2013 at 21:29
  • It seems to me they should be in one table with a related content_type table, but it is difficult to say for sure without knowing the full schema.
    – Bafsky
    Apr 2, 2013 at 21:32
  • The more capable the app, the more complex it is. If you are unsure about how you can manage complexity, then just go with the simpler schema. Once you feel you got the hang of it, you'll be ready to step up to a more complex system. At any rate, without a precise description of the schema, we won't be able to help you out with the complex queries.
    – didierc
    Apr 2, 2013 at 22:13

2 Answers 2

1

maybe think of using the "has a" relationship. For instance, an event "has a" "web item handle" attached to it, and a "web item handle" is a thing with description, created date, title, 'owner' etc...

0

Unless they truly have identical data, I would use separate tables. Having one table with some fields only used by specific content types is really not very good database design.

If you really want one table with the basic data, you could create one as you suggested with a content_type and the common fields, and then have 4 separate tables for each of the types with the other distinct fields, then do an inner join when you select the fields for that type. But personally I think you are better off just creating 4 tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.