在 Mysql(或任何)数据库中存储请求的正确方法
在数据库中存储请求的“正确”(最规范化?)方法是什么?例如,用户提交一篇文章。本文在发布到网站之前必须经过审查和批准。
哪种方法更合适:
A)将其存储在带有“已批准”字段的文章表中,该字段为 0、1、2(拒绝、批准、待定)
或
B)拥有具有相同字段的 ArticleRequests 表作为文章,并在获得批准后,将行数据从文章请求移动到文章。
谢谢!
What is the "proper" (most normalized?) way to store requests in the database? For example, a user submits an article. This article must be reviewed and approved before it is posted to the site.
Which is the more proper way:
A) store it in in the Articles table with an "Approved" field which is either a 0, 1, 2 (denied, approved, pending)
OR
B) Have an ArticleRequests table which has the same fields as Articles, and upon approval, move the row data from ArticleRequests to Articles.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于每篇文章都会有一个批准状态,并且每次请求一篇文章时,您很可能需要知道该状态 - 使其与表格保持一致。
不过,请考虑调用该字段
ApprovalStatus
。您可能需要添加一个相关表来包含每个状态,除非它们不会经常(或永远)更改。编辑:在相关表中保留字段的原因是:
null
。对于您的情况,上述原因不适用。
Since every article is going to have an approval status, and each time an article is requested you're very likely going to need to know that status - keep it inline with the table.
Do consider calling the field
ApprovalStatus
, though. You may want to add a related table to contain each of the statuses unless they aren't going to change very often (or ever).EDIT: Reasons to keep fields in related tables are:
null
.In your case those above reasons don't apply.
一定要做“A”。
如果您执行 B,您将创建一个与另一个表具有相同字段的新表,这意味着您做错了什么。你在重复自己。
Definitely do 'A'.
If you do B, you'll be creating a new table with the same fields as the other one and that means you're doing something wrong. You're repeating yourself.
我认为最好将具有特定状态的数据存储在主表中。因为如果此一项获得批准,则无需在表之间移动数据,并且该文章将同时出现在网站上。如果您不想存储未批准的文章,您应该创建 cron 脚本,删除不必要的数据或将它们移至存档表。在这种情况下,您的数据库负载将会减少,因为您可以调整适当的时间来删除旧文章,例如在晚上。
关于在每个查询中使用审批状态的问题:如果您计划拥有非常受欢迎的高负载站点来搜索或制作文章列表,您将使用独立服务器,例如 sphinx 或 solr(mysql 不是用于此目的的良好解决方案)并且您将把数据放入这些状态=“已批准”的状态。使用增量索引可帮助您保持数据最新。
I think it's better to store data in main table with specific status. Because it's not necessary to move data between tables if this one is approved and the article will appear on site at the same time. If you don't want to store disapproved articles you should create cron script with will remove unnecessary data or move them to archive table. In this case you will have less loading of your db because you can adjust proper time for removing old articles for example at night.
Regarding problem using approval status in each query: If you are planning to have very popular site with high-load for searching or making list of article you will use standalone server like sphinx or solr(mysql is not good solution for this purposes) and you will put data to these ones with status='Approved'. Using delta indexing helps you to keep your data up-to-date.