Question

I am trying to build a small web application. The application is to store the bookmarks and its tags.

bookmark: www.javaworld.com
tags: java, programming

bookmark: www.jquery.com

tags: java,webapps

now my search criteria is based on tags. If a user search's for java it should give all the bookmarks he stored with java. here both www.javaworld.com and www.jquery.com must be shown.

my database tables looks like this

Users ( UID, Uname, Password)

Bookmarks( BID, UID, bookmark_url,tag1,tag2,tag3,tag4,tag5)

or

Bookmarks( BID,UID,bookmark)

Tags(TID,BID,Tagname)

here BID and TID are surrogate keys.

please suggest the better way to solve this problem considering the scenario i gave..

Was it helpful?

Solution

Why not make something similar like on Stackoverflow? Tags are "global" and not bound to a single bookmark. Allows you to define tags without having to define bookmarks beforehand, which improves flexibility. Also limiting to 5 tags as in your first solution is not a good idea either.

Separating them like follows allows you to run aggregrate functions better (making tag clouds) and you can use the tag table to display tags in a checkbox/multiple select for new bookmarks better. It'd feel awkward if they are bound to another bookmark already.

Table: User [UID, Uname, PW]

Table: Bookmark [BID, UID, url]

Table: Tag [TID, Tagname]

Table: Bookmark_Tag [BID, TID]

OTHER TIPS

Your first solution has the following problems:
- you have a fixed number of tags: how can a user define a 6th tag for her bookmark?
- you are likely to have many NULL values: if a bookmark has less than 5 tags, the other columns will contain no values. In such a case you're likely to waste memory for the unused columns.
- in order to speed up the retrieval mechanism you should index the columns on which you want to perform a search (in your case the tag1,...tag5): you should index all 5 tag columns.

For such a reason I'd suggest you to choose the second solution, defining an index on "Tagname" attribute in the Tags table. Or, as an alternative, you may include a Tag table in which you define every tag. In such a case your Tags table will refer to the Tag table by using the numeric id (int) of the tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top