在mysql中将注释表映射到多个表的最佳实践是什么?
我希望找到将一个基表映射到多个表的最佳实践。例如,如果我有以下任何一个基表评论、标签、收藏夹、评级,它可以映射到一个或多个表,如博客文章、图片、视频。下面的例子可以提供更好的解释 更多信息: 我希望使用这些表来创建一个使用活动记录的Yii应用程序 我提议的解决方案: 我的基准表在mysql中将注释表映射到多个表的最佳实践是什么?,mysql,foreign-keys,comments,Mysql,Foreign Keys,Comments,我希望找到将一个基表映射到多个表的最佳实践。例如,如果我有以下任何一个基表评论、标签、收藏夹、评级,它可以映射到一个或多个表,如博客文章、图片、视频。下面的例子可以提供更好的解释 更多信息: 我希望使用这些表来创建一个使用活动记录的Yii应用程序 我提议的解决方案: 我的基准表 create table comment ( id int(4) unsigned not null auto_increment primary key, attach_id int(4) unsigned not
create table comment (
id int(4) unsigned not null auto_increment primary key,
attach_id int(4) unsigned not null, #used to attach to a specific post/photo/video
attach_type_id tinyint(1) unsigned not null, #foreign key to attach_type(id)
comment text not null,
user_id int(4) unsigned null,
datetime_added datetime not null,
foreign key (attach_type_id) references attach_type(id)
);
我的全局映射表:
create table attach_type (
id tinyint(1) unsigned not null auto_increment primary key,
table_name varchar(20) not null #used for reference purposes only
);
多个表中的两个的基本示例:
create table blog_post (
id int(4) unsigned not null auto_increment primary key,
title varchar(100) not null,
post text not null,
user_id int(4) unsigned null,
datetime_added datetime not null
);
create table photo (
id int(4) unsigned not null auto_increment primary key,
title varchar(100) not null,
description varchar(255) null,
file_name varchar(100) not null,
user_id int(4) unsigned null,
datetime_added datetime not null
);
检索博客帖子id=54的所有评论
blog\u post表在attach\u type表中的行的id=1
日志在blog_post表中的行的日志行id=54
select * from comments where attach_type_id=1 and attach_id=54;
因此,上面看到的评论、标签、收藏夹、评级、评论都可以附加到博客帖子和/或照片上。同样,可以将多条评论附加到单个博客上,因为帖子/照片允许多个用户发表评论。我的问题是在mysql中实现这一点的最佳方式是什么。上面的设置看起来合适吗?或者你会建议一个更好的方法吗?为什么。而且,如果使用上述解决方案,是否有人看到任何明显的缺点?提前感谢您的回复,我只是想找出最好的方法来做这件事
我相信这个话题与我的问题有关,但并没有真正回答我的问题:
提供了另一个问题的答案,我相信这回答了我的问题。我不知道如何给他适当的信任,但他的解决方案链接在这里:
汤姆,谢谢你的帮助
我仍然对建议持开放态度,但考虑到上面链接中发布的信息,我认为我将采用制作多个地图表的方法