开发者

Storing Categories in a Database

If some object can be a video, audio, or story is it better to have a separate table for each category or is it OK to have a "type" field that contains lots and lots of "audio", "video" and "story" entries?

1) ASSUME that each object can only be of one type

2) ASSUME that each object can be of multiple types (In this case it seems its best to define an ObjectType table that stores the relations between objects and types right?)开发者_如何学JAVA

(I am using a relational database)

Thanks!


Suggested schemas below. By using IDs rather than strings you lessen the amount of storage, and effort required by the database engine when joining. You also make it much easier to rename a category - rather than having to update umpteen rows where category is used, you update the one CATEGORY row.

With just one category per item:

ITEM:
ID
CATEGORYID
...

CATEGORY:
ID
NAME
...

With multiple categories per item:

ITEM:
ID
...

CATEGORY:
ID
NAME
...

ITEMCATEGORY:
ITEMID
CATEGORYID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜