构建数据库表的最佳方式是什么?

时间:2022-09-15 21:13:17

I'm having trouble designing my database schema.
I am having doubts on whether to separate or group tables.

我在设计数据库模式时遇到了麻烦。我对是否要分开或分组表有疑问。

I have a blog and a News section in my application and database. Both receive Comments and Likes.

我的应用程序和数据库中有一个博客和一个新闻部分。两者都能收到评论和喜欢。

TABLE.BLOG | TABLE.COMMENTS.BLOG | TABLE.LIKE.BLOG
TABLE.NEWS | TABLE.COMMENTS.NEWS | TABLE.LIKE.NEWS

I share everything?

我分享一切吗?

TABLE.BLOG | TABLE.NEWS
TABLE.COMMENTS | TABLE.LIKE

or should I keep comments grouped?

还是应该将评论分组?

In case I should have some kind of reference type blog | news?

万一我有某种参考类型的博客|新闻?

I am very confused about the best way to structure the database.
I really appreciate if someone can help me.

我对构建数据库的最佳方式感到非常困惑。如果有人能帮助我,我将不胜感激。

2 个解决方案

#1


2  

Although the exact layout depends mostly on the way that you are going to access and use your data, you are probably going to be better off with comments and likes sitting in the same table. Your second approach is close, although I would probably introduce a third table, called CONTENT, with an ID of anything that could be liked or commented.

虽然确切的布局主要取决于您访问和使用数据的方式,但是您最好还是使用注释,并且喜欢坐在同一个表中。您的第二种方法很接近,尽管我可能会引入第三个表,名为CONTENT,它带有任何可以被喜欢或注释的ID。

Each row in the NEWS and the BLOG table would have a corresponding row in the CONTENT table. A CONTENT row could correspond to either a NEWS or a BLOG, but not both. CONTENT table has attributes common to blogs and news (date, title, author, and so on).

新闻和博客表中的每一行在内容表中都有相应的行。内容行可以对应新闻或博客,但不能同时对应新闻或博客。CONTENT table具有与blog和news(日期、标题、作者等)相同的属性。

The LIKE and COMMENT tables would then be connected to the CONTENT table, so you would not need to duplicate these two tables for NEWS and for BLOG.

然后,LIKE和COMMENT表将连接到CONTENT表,因此您不需要为NEWS和BLOG复制这两个表。

Here is an illustration:

这里有一个例子:

构建数据库表的最佳方式是什么?

#2


0  

Best is subjective, though I'd have two tables:

Best是主观的,尽管我有两个表:

Content  (news and blog content, store like count)
Comment  (news and blog comments)

and then add a type field on both that distinguishes between blog and news. That way you can reuse a lot of the db code.

然后在两者上添加一个区分博客和新闻的类型字段。这样,您就可以重用许多db代码。

IOW, minimize the number of tables if you can.

现在,尽可能减少表的数量。

#1


2  

Although the exact layout depends mostly on the way that you are going to access and use your data, you are probably going to be better off with comments and likes sitting in the same table. Your second approach is close, although I would probably introduce a third table, called CONTENT, with an ID of anything that could be liked or commented.

虽然确切的布局主要取决于您访问和使用数据的方式,但是您最好还是使用注释,并且喜欢坐在同一个表中。您的第二种方法很接近,尽管我可能会引入第三个表,名为CONTENT,它带有任何可以被喜欢或注释的ID。

Each row in the NEWS and the BLOG table would have a corresponding row in the CONTENT table. A CONTENT row could correspond to either a NEWS or a BLOG, but not both. CONTENT table has attributes common to blogs and news (date, title, author, and so on).

新闻和博客表中的每一行在内容表中都有相应的行。内容行可以对应新闻或博客,但不能同时对应新闻或博客。CONTENT table具有与blog和news(日期、标题、作者等)相同的属性。

The LIKE and COMMENT tables would then be connected to the CONTENT table, so you would not need to duplicate these two tables for NEWS and for BLOG.

然后,LIKE和COMMENT表将连接到CONTENT表,因此您不需要为NEWS和BLOG复制这两个表。

Here is an illustration:

这里有一个例子:

构建数据库表的最佳方式是什么?

#2


0  

Best is subjective, though I'd have two tables:

Best是主观的,尽管我有两个表:

Content  (news and blog content, store like count)
Comment  (news and blog comments)

and then add a type field on both that distinguishes between blog and news. That way you can reuse a lot of the db code.

然后在两者上添加一个区分博客和新闻的类型字段。这样,您就可以重用许多db代码。

IOW, minimize the number of tables if you can.

现在,尽可能减少表的数量。