473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with Tags database design.

Hello,

I'm currently trying to decide on a database design for tags in my web 2.0 application. The problem I'm facing is that I have 3 separate tables i.e. cars, planes, and schools. All three tables need to interact with the tags, so there will only be one universal set of tags for the three tables.

I read a lot about tags and the best articles I found were:

Road to Web 2.0 ( http://wyome.com/docs/Road_to_Web_2.0:_The_Database_Design )
tags: database schema ( http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html )

and a forum discussion on tags with a very similar problem:
http://www.webmasterworld.com/forum112/502.htm
But I don't like the solution, would like to stick with serial integer for Cars, Planes and Schools tables.

Currently, this is my DB design:

Cars (carid, carname, text)
Planes (planeid, planename, text)
Schools (schoolname, text) <------ School does not take int as primary key but a varchar.

Tags (tagid, tagname)
--- Now here is where I have the question. I have to link up three separate tables to use Tags
--- So when a new car is created in the Cars table, should I insert that carID into the TagsItems table
--- as itemID? So something like this?

TagsItems
(
tagid INT NOT NULL REFERENCES Tags.TagID,
itemid INT NULL, <---- really references Cars.carID and Planes.planeID
schoolname varchar NULL <---- Saves the Schools.schoolname
itemid + tagId as Unique
)

I also have a question on the schoolname field, because it accepts varchar not integer. There seems to be some design that would better fit my needs. I'm asking you guys for a little assistance.
Jul 28 '07 #1
1 7096
Hi,

I found this website ( http://forge.mysql.com/wiki/TagSchema ) that took Philipp Keller's article ( http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html ) on tags and rewrote it will more examples. I still have a question on the best way to connect three tables using this method. But I hope my research can help those who have the same question. Here is the bit I like the most:


Schema

The schema involves three tables which represent the many to many relationship between the item and the tag. Here, we deviate from the bookmark paradigm and use a general "item" semantics to indicate that the type of item is irrelevant to the schema design.

CREATE TABLE Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, item_name VARCHAR(255) NOT NULL
/* Many more attributes of the item... */
, PRIMARY KEY (item_id)
) ENGINE=InnoDB;

CREATE TABLE Item2Tag (
item_id INT UNSIGNED NOT NULL
, tag_id INT UNSIGNED NOT NULL
, PRIMARY KEY (item_id, tag_id)
, INDEX (tag_id)
, FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id)
, FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;

CREATE TABLE Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, tag_text TEXT NOT NULL
, PRIMARY KEY (tag_id)
, UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;

[edit] Sample Schema with User Dimension

Additionally, when the application has a need for a user dimension (folksonomy), the schema can be expanded like so:

CREATE TABLE Users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, user_name VARCHAR(255) NOT NULL
/* Many more attributes of the user... */
, PRIMARY KEY user_id)
) ENGINE=InnoDB;

CREATE TABLE Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, item_name VARCHAR(255) NOT NULL
/* Many more attributes of the item... */
, PRIMARY KEY (item_id)
) ENGINE=InnoDB;

CREATE TABLE Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, tag_text TEXT NOT NULL
, PRIMARY KEY (tag_id)
, UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;

CREATE TABLE UserItemTag (
user_id INT UNSIGNED NOT NULL
, item_id INT UNSIGNED NOT NULL
, tag_id INT UNSIGNED NOT NULL
, PRIMARY KEY (user_id, item_id, tag_id)
, INDEX (item_id)
, INDEX (tag_id)
, FOREIGN KEY fk_User (user_id) REFERENCES Users (user_id)
, FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id)
, FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;

[edit] Sample Schema with Summary Tables

If an application requires aggregate data access patterns -- such as COUNTing the number of tags per item, per user/item combination, etc -- then we recommend denormalizing the schema to use aggregate or summary tables which store specific counters. This technique is used in order to bypass the current InnoDB-specific limitation of poorly performing aggregate queries. Below, we use the MEMORY storage engine for the statistic tables since this engine provides excellent speed for primary key access due to its hash-based indexing. The data for MEMORY tables is lost upon system shutdown; however since all the information in the summary tables can be rebuilt from the data in the main tables, there is no need for concern. For systems where memory requirements are an issue, simply change the MEMORY storage engine to InnoDB.

CREATE TABLE Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, item_name VARCHAR(255) NOT NULL
/* Many more attributes of the item... */
, PRIMARY KEY (item_id)
) ENGINE=InnoDB;

CREATE TABLE Item2Tag (
item_id INT UNSIGNED NOT NULL
, tag_id INT UNSIGNED NOT NULL
, PRIMARY KEY (item_id, tag_id)
, INDEX (tag_id)
, FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id)
, FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;

CREATE TABLE Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, tag_text TEXT NOT NULL
, PRIMARY KEY (tag_id)
, UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;

CREATE TABLE TagStat (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, num_items INT UNSIGNED NOT NULL DEFAULT 1
/* For each distinct type of item tracked with tags, add a field... */
, PRIMARY KEY (tag_id)
, FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=MEMORY;

--------------
Jul 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ian DeRock | last post by:
I'm fairly new to XML. As I can see it, XML is a way to organize data like in a table in a RDB, or the RDB itself. I have used XML in application data, but did not design the actual XML. I...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
5
by: trode | last post by:
Hello all. For the first time, our company might make use of the MS Access portion of the office package. Problem is, I don't know it well enough to whip out a desired program. What we want to...
4
by: lorirobn | last post by:
Hello, I'd be curious to hear other thoughts on my database and table design. My database is for 'space use' in a lodging facility - will hold all spaces, like rooms (lodging rooms, dining...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
0
by: tekhneek | last post by:
I'm building a site for my family & friends to all keep in touch and the two features I'm coming to a halt about is this (in database design) I let users create their own pages (with a title and...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: shapper | last post by:
Hi, On a form I have an input where tags are inserted in a CSV format. Then on my code I convert the CSV string to a List<Tag>. Tag is an object with two properties: TagID and Name So when I...
1
by: shapper | last post by:
Hello, I am using the following: PostPaper paper = (from p in database.Posts where p.PostID == id select new PostPaper { Post = p, Tags = (from pt in database.PostsTags join t in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.