在mysql数据库中存储关系

发布于 2024-10-16 05:16:55 字数 777 浏览 5 评论 0原文

我正在内容之间创建多对多关系,但为了简化它,我现在将使用更简单的关系。一个例子是电影之间的关系。存储这些数据的正确方法是什么?

我最初是这样做的:

Movie | Related Movie | Relation (Relation of the related movie)
--------------------------------
Matrix   | Matrix 2 | Sequel
Matrix 2 | Matrix   | Prequel

所以《黑客帝国 2》是《黑客帝国》的续集,但后来我意识到存储相关电影的关系而不是实际电影的关系似乎没有意义。所以然后我尝试了这个:

Movie | Relation | Related Movie 
--------------------------------
Matrix   | Prequel | Matrix 2
Matrix 2 | Sequel  | Matrix

现在我存储电影的实际关系而不是相关电影,因此该行更有意义。它也更字面化,《黑客帝国》是《黑客帝国 2》的前传。

但是,后来我意识到在前端使用第二种方式,《黑客帝国》页面看起来像这样: 前传 - 《黑客帝国 2》

以及《黑客帝国 2》页面: 续集 - 矩阵

所以第一种方式似乎在后端更正确地存储数据,但不是前端。而第二种方式似乎在后端没有正确存储数据,但在前端它更有意义。

那么在这种情况下,我实际上应该以相反的方式存储数据(第二种方式)吗?我应该担心这个吗?只要在前端有意义就行?

I'm creating a many to many relationship between content, but to simplify it I'll use an easier relationship for now. An example would be a relationship between movies. What would be the right way to store this data?

I was originally doing this:

Movie | Related Movie | Relation (Relation of the related movie)
--------------------------------
Matrix   | Matrix 2 | Sequel
Matrix 2 | Matrix   | Prequel

So Matrix 2 is the sequel of Matrix, but then I realized it doesn't seem to make sense to store the relation of the related movie instead of the relation of the actual movie. So then I tried this instead:

Movie | Relation | Related Movie 
--------------------------------
Matrix   | Prequel | Matrix 2
Matrix 2 | Sequel  | Matrix

Now I'm storing the actual relation of the movie instead of the related movie, so the row makes more sense. It's also more literal, Matrix is the prequel of Matrix 2.

However, then I realized in the front end using the 2nd way it would look like this for the Matrix page:
Prequel - Matrix 2

And for the Matrix 2 page:
Sequel - Matrix

So the first way seems to store the data more correctly in the backend, but not the front end. And the second way seems that it doesn't store the data correctly in the backend, but in the front end it makes more sense.

So in this case, should I actually store the data the other way around (2nd way)? Should I even be concerned about this at all? As long as it makes sense in the front end?

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

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

发布评论

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

评论(1

冷清清 2024-10-23 05:16:55

您无需同时存储后向和前向关系。与链表不同,您不必存储 prevnext 指针即可向后和向前遍历。

例如,为什么不这样做:

SELECT * FROM movies;
+----+---------------+
| id | title         |
+----+---------------+
|  1 | Matrix        |
|  2 | Matrix 2      |
|  3 | The Animatrix |
+----+---------------+

SELECT * FROM movie_relations;
+----+----------+---------------+------------------+
| id | movie_id | relation_type | related_movie_id |
+----+----------+---------------+------------------+
|  1 |        1 | sequel        |                2 |
|  2 |        1 | offshoot      |                3 |
|  3 |        2 | offshoot      |                3 |
+----+----------+---------------+------------------+

现在,如果您需要查找《黑客帝国》的所有续集:

SELECT related_movie_id FROM movie_relations 
WHERE movie_id = 1 AND relation_type = 'sequel'

如果您需要查找《黑客帝国 2》的所有前传,您知道这只是带有 lated_movie_id 的电影列表2 作为续集:

SELECT movie_id FROM movie_relations 
WHERE related_movie_id = 2 AND relation_type = 'sequel'

假设您需要与《黑客帝国 2》相关的所有电影(它具有隐式前传和直接指定的分支):

SELECT DISTINCT(movies.id), title FROM movies 
    LEFT JOIN movie_relations mr_direct ON mr_direct.related_movie_id = movies.id 
    LEFT JOIN movie_relations mr_implicit ON mr_implicit.movie_id = movies.id 
WHERE mr_direct.movie_id = 2 OR mr_implicit.related_movie_id = 2;

+----+---------------+
| id | title         |
+----+---------------+
|  1 | Matrix        |
|  3 | The Animatrix |
+----+---------------+

查询比存储冗余数据稍微复杂一些。但是,我宁愿不要在不必要的地方重复信息。

You needn't store both the backwards and forwards relations. Unlike a linked list, you don't have to store the prev and next pointers to be able to traverse backwards and forwards.

For example, why not do:

SELECT * FROM movies;
+----+---------------+
| id | title         |
+----+---------------+
|  1 | Matrix        |
|  2 | Matrix 2      |
|  3 | The Animatrix |
+----+---------------+

SELECT * FROM movie_relations;
+----+----------+---------------+------------------+
| id | movie_id | relation_type | related_movie_id |
+----+----------+---------------+------------------+
|  1 |        1 | sequel        |                2 |
|  2 |        1 | offshoot      |                3 |
|  3 |        2 | offshoot      |                3 |
+----+----------+---------------+------------------+

Now, if you need to find all sequels of Matrix:

SELECT related_movie_id FROM movie_relations 
WHERE movie_id = 1 AND relation_type = 'sequel'

If you need to instead find all prequels of Matrix 2, you know that this is simply the list of movies with related_movie_id 2 as a sequel:

SELECT movie_id FROM movie_relations 
WHERE related_movie_id = 2 AND relation_type = 'sequel'

Let's say you need all movies related to Matrix 2 (which has an implicit prequel and a directly specified offshoot):

SELECT DISTINCT(movies.id), title FROM movies 
    LEFT JOIN movie_relations mr_direct ON mr_direct.related_movie_id = movies.id 
    LEFT JOIN movie_relations mr_implicit ON mr_implicit.movie_id = movies.id 
WHERE mr_direct.movie_id = 2 OR mr_implicit.related_movie_id = 2;

+----+---------------+
| id | title         |
+----+---------------+
|  1 | Matrix        |
|  3 | The Animatrix |
+----+---------------+

The query is slightly more complex than if you had stored redundant data. But, I prefer to not duplicate information where unnecessary.

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