在mysql数据库中存储关系
我正在内容之间创建多对多关系,但为了简化它,我现在将使用更简单的关系。一个例子是电影之间的关系。存储这些数据的正确方法是什么?
我最初是这样做的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需同时存储后向和前向关系。与链表不同,您不必存储
prev
和next
指针即可向后和向前遍历。例如,为什么不这样做:
现在,如果您需要查找《黑客帝国》的所有续集:
如果您需要查找《黑客帝国 2》的所有前传,您知道这只是带有
lated_movie_id
的电影列表2 作为续集:假设您需要与《黑客帝国 2》相关的所有电影(它具有隐式前传和直接指定的分支):
查询比存储冗余数据稍微复杂一些。但是,我宁愿不要在不必要的地方重复信息。
You needn't store both the backwards and forwards relations. Unlike a linked list, you don't have to store the
prev
andnext
pointers to be able to traverse backwards and forwards.For example, why not do:
Now, if you need to find all sequels of Matrix:
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:Let's say you need all movies related to Matrix 2 (which has an implicit prequel and a directly specified offshoot):
The query is slightly more complex than if you had stored redundant data. But, I prefer to not duplicate information where unnecessary.