duancao2082 2016-01-01 03:09
浏览 18
已采纳

使用PHPMyAdmin并创建一个包含多个条目的列? [关闭]

I'm creating a database for the first time! The goal is to have a database for users. I'm using PHPMyAdmin on my localhost (XAMPP) to set it up.

This is pretty straightforward for things like "username" and "password", but one of the fields would be a list of activities that the user participates in.

So for instance, "Activities: Skiing, Rowing, Swimming" would be what this would look like on their page.

But I am not sure how to set this up on PHPMyAdmin. I can't just create a field called "Activities" of type VARCHAR(255) because I think it would store their interests in one box, which isn't good because I need to have these separatedly sorted for later access (For instance, I'd need to query all the users that have the "Swimming" activity in common).

What approach should I ideally be using?

Thank you!

  • 写回答

1条回答 默认 最新

  • dongmeiyi2266 2016-01-01 03:38
    关注

    Table users

    ---------------------------------------
    | id |  username  |     password      |
    ---------------------------------------
    | 1  |    john    |  somehashedpass1  |
    | 2  |    carl    |  somehashedpass2  |
    | 3  |    maria   |  somehashedpass3  |
    ---------------------------------------
    

    Table activities

    --------------------------------------
    | id |  user_id  |     activity      |
    --------------------------------------
    | 1  |     1     |      Skiing       |
    | 2  |     1     |      Rowing       |
    | 3  |     1     |     Swimming      |
    | 4  |     2     |      Skiing       |
    | 5  |     2     |      Rowing       |
    | 6  |     3     |      Skiing       |
    --------------------------------------
    

    Then use JOIN or Sub-queries to fetch the required data.

    Also, you could implement a 3-tables schema to define users, activities and relationships, like this:

    Table users (no variation)

    ---------------------------------------
    | id |  username  |     password      |
    ---------------------------------------
    | 1  |    john    |  somehashedpass1  |
    | 2  |    carl    |  somehashedpass2  |
    | 3  |    maria   |  somehashedpass3  |
    ---------------------------------------
    

    Table activities

    -----------------
    | id | activity |
    -----------------
    | 1  |  Skiing  |
    | 2  |  Rowing  |
    | 3  | Swimming |
    -----------------
    

    Table relationships

    ------------------------------
    | id | user_id | activity_id |
    ------------------------------
    | 1  |    1    |      1      |
    | 2  |    1    |      2      |
    | 3  |    1    |      3      |
    | 4  |    2    |      1      |
    | 5  |    2    |      2      |
    | 6  |    3    |      1      |
    ------------------------------
    

    This will be basicaly the same as the previous implementation, but you'll have a better structured database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案