tags 的数据库设计

来源:互联网 发布:怎么让mac自动获取dns 编辑:程序博客网 时间:2024/04/29 16:54

我想做个网站,包含tag标签的,相信大家都见过   但不知道数据库怎么设计比较合理  。今天搜索了下

总算有思路了。

如果TAG没有层次关系就很简单,只需要建立一个TAG表,用户输入TAG后先查询该表,如果已经有,就不用再加入,如果没有就加了  
因为有很多同义词和标准用语的问题,所以最好在前台设置提示,智能提示功能,如输入电,就会出现已经有的TAG,用户只要选择电影或者电视就可以

引用一篇文章 原文:http://www.21andy.com/blog/20071222/769.html

说说wordpress和sablog在tag数据库结构设计和程序方面的差别

sablog的sablog_articles表

 

`keywords` varchar(120) NOT NULL default '',


keywords字段: 以 "," 分隔所有这篇文章使用到的tag

sablog的sablog_tags表

 

CREATE TABLE IF NOT EXISTS `sablog_tags` (
`tagid` 
int(11) unsigned NOT NULL auto_increment,
`tag` 
varchar(100NOT NULL default '',
`usenum` 
int(11NOT NULL default '0',
`aids` 
text NOT NULL,
PRIMARY KEY  (`tagid`),
KEY `usenum` (`usenum`)
) ENGINE
=MyISAM  DEFAULT CHARSET=utf8  ;

 

tag字段: 存放tag名称
usenum字段: 存放使用这个tag的文章总数
aids字段: 以 "," 分隔所有使用这个tag的文章id

显示文章tag的时候,是这么写的

 

$tagdb = explode(',', $article['keywords']);
            
$articletags = $tmark = '';
            
for($i=0$i<count($tagdb); $i++) {
                
$tagdb[$i= trim($tagdb[$i]);
                
$articletags .= $tmark.'<a href="./?action=tags&amp;item='.urlencode($tagdb[$i]).'">'.htmlspecialchars($tagdb[$i]).'</a>';
                
$tmark = '';
            }
            
$article['tags'= $articletags;

 

取出sablog_articles表keywords字段,explode后生成链接

显示相关文章的程序部份

 

$tags = $comma = '';
                
for($i=0$i<count($tagdb); $i++) {
                    
$tags .= $comma."'".addslashes($tagdb[$i])."'";
                    
$comma = ',';
                }
                
$query = $DB->query("SELECT aids FROM {$db_prefix}tags WHERE tag IN ($tags)");
                
$relaids = 0;
                
while ($tag = $DB->fetch_array($query)) {
                    
$relaids .= ','.$tag['aids'];
                }
                
$relids = explode(',', $relaids);
                
// 清除重复值的单元并删除当前ID
                $relids = array_unique($relids);
                
$relids = array_flip($relids);
                
unset($relids[$articleid]);
                
$relids = array_flip($relids);
                
////////
                $related_tatol = count($relids);
                
$relids = implode(',',$relids);
                
if ($related_tatol > 1 && $relids != $articleid) {
                    
$order = in_array($options['related_order'], array('dateline', 'views', 'comments')) ? $options['related_order': 'dateline';
                    
$query = $DB->query("SELECT articleid,title,views,comments FROM {$db_prefix}articles WHERE visible='1' AND articleid IN ($relids) ORDER BY ".$order." DESC LIMIT ".intval($options['related_shownum']));
                    
$titledb=array();
                    
while ($title = $DB->fetch_array($query)) {
                        
$title['title'= trimmed_title($title['title'], $options['related_title_limit']);
                        
$titledb[] = $title;
                    }
                    
unset($title);
                    
$DB->free_result($query);
                }

 

取出sablog_articles表keywords字段,生成取得aids的查询条件

 

$query = $DB->query("SELECT aids FROM {$db_prefix}tags WHERE tag IN ('tag1','tag2','tag3')");


从而得到所有包含tag1,tag2,tag3的文章id, 注意aids字段的内容,上面我已经说明了aids字段: 以 "," 分隔所有使用这个tag的文章id

wordpress则用到了4张表wp_posts, wp_terms, wp_term_relationships, wp_term_taxonomy
wordpress在新版里,数据库结构有了调整,结构和名称与以前的不一样,category和tag合并了,区别在wp_term_taxonomy里taxonomy字段
category则taxonomy字段内容为category
tag则taxonomy字段内容为post_tag
wp_posts里不存放任何tag数据
wp_terms表用于存放tag和category,结构如下

 

CREATE TABLE IF NOT EXISTS `wp_terms` (
`term_id` 
bigint(20NOT NULL auto_increment,
`name` 
varchar(55NOT NULL default '',
`slug` 
varchar(200NOT NULL default '',
`term_group` 
bigint(10NOT NULL default '0',
PRIMARY KEY  (`term_id`),
UNIQUE KEY `slug` (`slug`)
) ENGINE
=MyISAM  DEFAULT CHARSET=utf8;

 

`wp_terms`里的 term_id 和 `wp_term_taxonomy` 里的 term_id 关联
`wp_posts`里的 ID 和 wp_term_relationships 里的 object_id 关联
`wp_term_relationships`里的 term_taxonomy_id 和 `wp_terms` 里的 term_id 关联
先说到这吧,具体的去看数据库,熟练的程序员,由数据库结构,就能知道程序是怎么写的了

wordpress的tag数据库结构要清晰条理得多,sablog的tag数据库结构就不太利于维护,但对于2者在显示方面的性能,我还没有做测试