软删除用户表?或者还有其他选择吗?

发布于 2024-12-07 02:46:44 字数 405 浏览 1 评论 0原文

读完所有专业人士的文章后我的头晕了。 con 关于软删除的文章。但这是我知道实现这一目标的唯一方法:

<块引用>

维护外键&用户信息(历史数据) 即使用户被删除/不活动,评论、附件和故事表中也有外键。这样仍然可以识别出这条评论是他写的等等。

其他信息:

<块引用>

停用的用户无法登录,不会包含在列表中。

但是如果我使用软删除,那么每次查询该表时在 SQL 语句中的 WHERE 中添加额外的列并不是很好。

该怎么办?希望大家能够给予一些意见。

注意:我使用mysql和ROR

My head's spinning from reading all the pro's & con's articles about soft-delete. But that's the only way I know to achieve this:

maintain the foreign keys & user info (history data)
Even if the user is deleted/inactive, there are foreign keys in comments, attachments and stories tables. So that it can still be identified that it was him who wrote this comment etc.

Other info:

deactivated users cannot login, won't be included in lists.

But if I use soft delete, it's not really good adding an extra column in the WHERE in sql statement every time I query for that table.

What to do? Hope you guys can give some inputs.

Note: I use mysql and ROR

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-12-14 02:46:44

如果“软删除”用户,则无需在每个 SQL 语句的 WHERE 子句中添加额外的列。相反,构建一个仅返回活动用户的视图,并且需要了解活动用户的查询使用该查询。他们不使用基表。

它可能看起来像这样。

create view users_active as
select * from users
where deleted = false;

根据到目前为止您已完成的开发量,您可能需要重命名用户表,并将新视图命名为“users”。类似的事情。 。 。

alter table users rename to users_all;

create view users as
select user_id, user_name from users_all   -- All columns except "deleted"
where deleted = false;

现在,任何用于命中表“users”的查询都将命中视图“users”。逻辑数据独立性——这就是视图和表在关系模型中共享相同名称空间的原因。

If you "soft delete" users, you don't add an extra column in the WHERE clause of every SQL statement. Instead, build one view that returns only active users, and the queries that need to know about active users use that query. They don't use the base table.

It might look something like this.

create view users_active as
select * from users
where deleted = false;

Depending on how much development you've done so far, you might want to rename the users table, and name the new view "users" instead. Something along these lines . . .

alter table users rename to users_all;

create view users as
select user_id, user_name from users_all   -- All columns except "deleted"
where deleted = false;

Now any query that used to hit the table "users" will instead hit the view "users". Logical data independence--this is why views and tables share the same namespace in the relational model.

月亮坠入山谷 2024-12-14 02:46:44

这个世界上实现目标的方法有很多,不要认为某人说的总是正确的:)

首先,说到评论 - 是的,如果您要加入用户表来获取发送评论的人的用户名评论,使用软删除 - 是的,要知道谁写了评论要容易得多。
检查它们是否被删除也很容易,因此您也可以将其呈现为 HTML。
另一方面,您还可以将用户名存储在 comments 表中,这样就可以避免加入 users 表。但是,很难知道用户是否已被删除,因为您没有从用户表中收到该信息。

在 WHERE 中添加附加子句不会杀死任何人。 SQL 的诞生是为了让你得到你想要的数据,计算机满足用户的需求,而不是相反。因此,如果您认为软删除是您所需要的,请使用它。唯一的“真正”的缺点是您将记录保留在数据库中,因此有人可能会说软删除会导致数据库膨胀。

There are so many ways to achieve things in this world, don't think what someone said is always true :)

First off, when it comes to comments - yes, if you are joining users table to obtain username for the person who sent the comment, using soft delete - yes, it's much, much easier to obtain who wrote the comment.
It's also easy to check whether they're deleted so you can render it in HTML too.
On the other hand, you can also store username in the comments table so you can avoid joining the users table. However, it's difficult to know whether the user was deleted or not since you're not receiving that information from users table.

Adding additional clause in WHERE won't kill anyone. SQL was made so you get the data you want, computers conform to users' needs, not the other way around. So, if you think soft-delete is what you need, use it. The only "real" con is that you keep the record in the db, so someone might say that soft deletes inflate databases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文