MySQL 创建表之间的关系

发布于 2024-12-20 01:22:16 字数 606 浏览 2 评论 0原文

如果我有两个表:

    *******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 技术交流群。

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

发布评论

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

评论(1

娇女薄笑 2024-12-27 01:22:16

这取决于您想要建立什么类型的关系。从你的问题来看,我认为你的意思是多对多的关系(即相同的奖励可以授予多个用户,并且一个用户可以拥有多个奖励)。对此进行建模的最佳方法确实是第三个表,它管理用户及其奖励之间的关系。

例如,请参阅此网站解释设计模式: 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.

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