MySQL 创建表之间的关系
如果我有两个表:
*******awards
----------------------
aid | position | name
----------------------
1 1 best game
2 2 reached 100 points
3 3 reached 300 points
................
1000 800 logged_3_days_in_a_row
*******users
----------------------
uid | name
----------------------
1 Niki
2 Lulu33
3 BadGirl
.......
1001 Zelda2012
在奖励表和用户之间建立关系的最佳方法是什么?当然,我可以创建一个 user_has_awards
表,但我不确定这是否是执行此操作的最佳方式(如果有 1000 个或更多奖项和 1000 个用户)。
If I have two tables:
*******awards
----------------------
aid | position | name
----------------------
1 1 best game
2 2 reached 100 points
3 3 reached 300 points
................
1000 800 logged_3_days_in_a_row
*******users
----------------------
uid | name
----------------------
1 Niki
2 Lulu33
3 BadGirl
.......
1001 Zelda2012
What is the best way to make a relation between the awards table and the users? Of course I could create a user_has_awards
table but I'm not sure if that's the best way (if there are 1000 or more awards and 1000 users) to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您想要建立什么类型的关系。从你的问题来看,我认为你的意思是多对多的关系(即相同的奖励可以授予多个用户,并且一个用户可以拥有多个奖励)。对此进行建模的最佳方法确实是第三个表,它管理用户及其奖励之间的关系。
例如,请参阅此网站解释设计模式: http://www.tomjewett .com/dbdesign/dbdesign.php?page=manymany.php
PS。您应该开始接受一些问题的答案,以鼓励人们回答您的问题。
It depends on what type of relationship you want to establish. From your question I think you mean a many-to-many relationship (i.e. the same award can be given to many users and a user can have many awards). The best way to model this is indeed a third table which manages the relations between users and their awards.
See for instance this site explaining the design pattern: http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php
PS. you should start accepting some of the answers to your questions to encourage people to answer your questions.