Q:

MySQL & PHP Tagging

I'm creating a webapp.  I want to support tags.  How do I set up the MySQL table?  I want to relate a link with various tags. 

A:

The following format allows multiple tags per link and allows links to share the same tags.  This allows you to easily see which links are popular, and at the same time avoid storing duplicate tags. 
Schema
Links
  LinkID
  Url
  Name

Tags
  TagID
  Name

LinkTags
  LinkID
  TagID

Sample data:
Links
---------------------------------------------------
LinkID      URL                    Name
1           http://avira.com/      Avira Antivirus
2           http://microsoft.com/  Microsoft
---------------------------------------------------

Tags
---------------------------------------------------
TagID       Name
---------------------------------------------------
1           Software
2           Antivirus
3           Operating Systems
---------------------------------------------------

LinkTags
---------------------------------------------------
LinkID      TagID
---------------------------------------------------
1           1
1           2
2           1
2           3

Queries
Links and tags:
SELECT Links.*,  GROUP_CONCAT(Tags. Name) AS TagList
FROM Links
LEFT JOIN LinkTags
  ON LinkTags. LinkID = Links. LinkID
LEFT JOIN Tags
  ON Tags. TagID = LinkTags. TagID
GROUP BY Links. LinkID

Ten most popular tags:
SELECT Tags.* 
FROM LinkTags
LEFT JOIN Tags
  ON Tags. TagID = LinkTags. TagID
GROUP BY LinkTags. TagID
ORDER BY (COUNT(LinkTags. LinkID)) DESC
LIMIT 10

