请教一个数据库设计表问题

WBOY
Release: 2016-06-06 20:48:46
Original
931 people have browsed it

说一个小微博系统,用的mysql数据库, 例如微博表结构如下: content(文字内容), photos(图片), video(视频), dateCreated(发布日期)

问题是这样的, 我发一条微博后, 如何判断这个微博是否带视频,或者图片呢?

方案1:通过判断photos,video 是否为空? 方案2:增加2个字段判断是否photos,video为空? 例如0代表空,1代表有

我不知道第一种方式是否速度上有问题? 或者有其他更好的方法。

烦请大侠们帮忙,谢谢

回复内容:

说一个小微博系统,用的mysql数据库, 例如微博表结构如下: content(文字内容), photos(图片), video(视频), dateCreated(发布日期)

问题是这样的, 我发一条微博后, 如何判断这个微博是否带视频,或者图片呢?

方案1:通过判断photos,video 是否为空? 方案2:增加2个字段判断是否photos,video为空? 例如0代表空,1代表有

我不知道第一种方式是否速度上有问题? 或者有其他更好的方法。

烦请大侠们帮忙,谢谢

再建一个附件表,id, resourceType, attachFile ,
另一个关联表结构为id, contentId, resourceId, resourceType

<code class="lang-sql">#查看是否带有图片
select count(*) from 关联表 r where r.contentId = ? and r.resourceType = 0
</code>
Copy after login

这么设计原因是:

  • 从数据库设计角度来说,在一张表里最好是一个实体及其属性。
  • 一条微博有多张图片时,不需要在内容表里,再添加一条内容重复,只是附件不同的的记录。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!