下面是一个简单的设计方案:
1. 文章表的基本结构
create table tb_article (
id uuid not null,
tags varchar(250) not null, # 由逗号分隔的多个tags,在界面上显示,不需要查询关联表。
title varchar(250) not null,
content text not null,
primary key (id)
)
2. Tag的关联表
create table tb_tags_rel (
id uuid not null,
tag varchar(50) not null, # 单个tag
tag_type enum not null, # Tag分类:新闻/下载文档/...
target_id uuid not null, # 对应文章的ID
primary key (id)
)
这个表存储每个tag管理的对应文章,主要用来根据Tag进行查找。
可以实现像 http://www.oschina.com/article/tag/java 这样的URL
3. Tag的统计表
create table tb_tags_stat (
id uuid not null,
tag varchar(50) not null,
tag_type enum not null,
tag_desc varchar(50) null, # Tag 的描述
tag_color varchar(10) null, # Tag 显示的颜色
target_count int not null, # 某个分类下的tag对应的文章数量
tag_hits int not null, # 某个分类下的tag被点击的次数
primary key (id)
unique index (tag, tag_type)
)
这个表主要存储每个tag对应分类的统计信息。
可以列出每个分类下所有的tag,以及每个tag包含的文章数量。
当然,这里只是一个设计思路,欢迎大家讨论。
(全文完)