如何在 MySQL 中构建一个非常简单的博客的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12354947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:47:18  来源:igfitidea点击:

How to structure the tables of a very simple blog in MySQL?

mysqldatabasedatabase-designblogs

提问by ProgrammerGirl

I want to add a very simple blog feature on one of my existing LAMP sites. It would be tied to a user's existing profile, and they would be able to simply input a title and a body for each post in their blog, and the date would be automatically set upon submission. They would be allowed to edit and delete any blog post and title at any time. The blog would be displayed from most recent to oldest, perhaps 20 posts to a page, with proper pagination above that. Other users would be able to leave comments on each post, which the blog owner would be allowed to delete, but not pre-moderate. That's basically it. Like I said, very simple.

我想在我现有的一个 LAMP 网站上添加一个非常简单的博客功能。它将与用户现有的个人资料相关联,他们将能够简单地为他们博客中的每篇文章输入标题和正文,并且日期将在提交时自动设置。他们可以随时编辑和删除任何博客文章和标题。博客将从最近到最旧显示,大约 20 个帖子到一个页面,上面有适当的分页。其他用户将能够对每个帖子发表评论,博客所有者将被允许删除,但不能预先审核。基本上就是这样。就像我说的,非常简单。

How should I structure the MySQL tables for this?

我应该如何为此构建 MySQL 表?

I'm assuming that since there will be blog posts and comments, I would need a separate table for each, is that correct? But then what columns would I need in each table, what data structures should I use, and how should I link the two tables together (e.g. any foreign keys)?

我假设因为会有博客文章和评论,我需要为每个单独的表格,对吗?但是每个表中我需要哪些列,我应该使用什么数据结构,以及我应该如何将两个表链接在一起(例如任何外键)?

I could not find any tutorials for something like this, and what I'm looking to do is really offer my users the simplest version of a blog possible. No tags, no moderation, no images, no fancy formatting, etc. Just a simple diary-type, pure-text blog with commenting by other users.

我找不到任何关于此类的教程,我想要做的是真正为我的用户提供最简单的博客版本。没有标签,没有节制,没有图片,没有花哨的格式等。只是一个简单的日记类型的纯文本博客,其他用户的评论。

回答by Carlos Grappa

I'd say you need the following tables

我会说你需要以下表格

Posts
  PostID (identity)
  PostTitle (varchar)
  PostDate (datetime)
  Deleted (int)
  OwnerID (int FK to Users)

PostDetails
  PostDetailID (identity)
  PostID (FK to Posts)
  Sequence (int) -> for long posts you order by this
  PostText (text)

Comments
  CommentID (identity)
  Comment (text)
  CommenterID (int FK to Users)
  CommentDate (datetime)
  Deleted (int)

Users
  UserID (identity)
  UserNAme (varchar)
  UserEmail (varchar)
  CreatedDate (datetime)
  Active (int)

All datetime fields default to the current time, all identity fields are PK The sequence field in post details is there in case you don't use the text type and go with varchar so you can split a post over several records.

所有日期时间字段默认为当前时间,所有身份字段都是 PK 帖子详细信息中的序列字段,以防您不使用文本类型并使用 varchar 以便您可以将帖子拆分为多个记录。

Other than this, I'd look at any open source blogging system and see what they did and subtract what I don't need.

除此之外,我会查看任何开源博客系统,看看他们做了什么,然后减去我不需要的东西。

Hope that helps

希望有帮助