<bdo id='5k2n6'></bdo><ul id='5k2n6'></ul>

<small id='5k2n6'></small><noframes id='5k2n6'>

<i id='5k2n6'><tr id='5k2n6'><dt id='5k2n6'><q id='5k2n6'><span id='5k2n6'><b id='5k2n6'><form id='5k2n6'><ins id='5k2n6'></ins><ul id='5k2n6'></ul><sub id='5k2n6'></sub></form><legend id='5k2n6'></legend><bdo id='5k2n6'><pre id='5k2n6'><center id='5k2n6'></center></pre></bdo></b><th id='5k2n6'></th></span></q></dt></tr></i><div id='5k2n6'><tfoot id='5k2n6'></tfoot><dl id='5k2n6'><fieldset id='5k2n6'></fieldset></dl></div>

    <tfoot id='5k2n6'></tfoot>

    1. <legend id='5k2n6'><style id='5k2n6'><dir id='5k2n6'><q id='5k2n6'></q></dir></style></legend>

      MySQL好友表

      MySQL friends table(MySQL好友表)
        <legend id='c4nOp'><style id='c4nOp'><dir id='c4nOp'><q id='c4nOp'></q></dir></style></legend>
        <i id='c4nOp'><tr id='c4nOp'><dt id='c4nOp'><q id='c4nOp'><span id='c4nOp'><b id='c4nOp'><form id='c4nOp'><ins id='c4nOp'></ins><ul id='c4nOp'></ul><sub id='c4nOp'></sub></form><legend id='c4nOp'></legend><bdo id='c4nOp'><pre id='c4nOp'><center id='c4nOp'></center></pre></bdo></b><th id='c4nOp'></th></span></q></dt></tr></i><div id='c4nOp'><tfoot id='c4nOp'></tfoot><dl id='c4nOp'><fieldset id='c4nOp'></fieldset></dl></div>

          <small id='c4nOp'></small><noframes id='c4nOp'>

            <bdo id='c4nOp'></bdo><ul id='c4nOp'></ul>
              <tbody id='c4nOp'></tbody>
              • <tfoot id='c4nOp'></tfoot>
                本文介绍了MySQL好友表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 MySQL 数据库,我在其中存储有关每个用户的数据.

                我想为每个用户添加一个朋友列表.我应该为数据库中的每个用户创建一个朋友表还是有更好的方法?

                解决方案

                假设你所有的朋友也在用户表中,你将需要一个朋友表,它定义了一个简单的一对多关系 - 将用户表链接回本身.所以

                用户表UserID int 身份不为空[其他属性字段]朋友桌UserIDLink1 intUserIDLink2 int[其他属性字段]

                其中 UserIDLink1 和 UserIDLink2 都是用户表上的外键.

                例如,如果我有三个用户

                1 乔2 比尔3 简

                Joe 和 Jane 是朋友,那么 Friends 表将包含一行

                1 3

                以上隐含假设,如果 A 是 B 的朋友,那么 B 是 A 的朋友 - 如果不是这种情况,您可能希望将 UserIDLink1 和 UserIDLink2 重命名为 UserID 和 FriendID 或类似名称 - 在这种情况下您也可以将记录翻倍.

                同样对于双向配置(如果 B 是 A 的朋友,A 是 B 的朋友)你应该在 Friends 表上为 (UserIDLink1,UserIDLink2) 和 (UserIDLink2,UserIDLink1) 设置索引以确保访问是如果我们搜索 joe 的朋友或 jane 的朋友,总是有效的(如果您没有设置第二个索引,那么第一个查询将是一个有效的索引查找,但第二个将需要全表扫描).

                如果您的链接不是双向的,就没有必要找出 A 的朋友是谁,但您可能仍然最需要它,因为您可能还需要找出 B 的朋友.

                I have a MySQL DB in which I store data about each user.

                I would like to add a list of friends for each user. Should I create a table of friends for each user in the DB or is there a better way?

                解决方案

                Assuming all your friends are also in the user table you will need a friends table which defines a simple one-to-many relationship - linking the users table back to itself. So

                User Table
                UserID int identity not null
                [other attribute fields]
                
                Friends Table
                UserIDLink1 int
                UserIDLink2 int 
                [other attribute field]
                

                Where both UserIDLink1 and UserIDLink2 are foreign keys on the Users table.

                So for instance if I have three users

                1 Joe
                2 Bill
                3 Jane
                

                and Joe and Jane are friends then the Friends table would contain a single row

                1 3
                

                The above implicitly assumes that if A is a friend of B then B is a friend of A - if this isn't the case you'd probably want to rename UserIDLink1 and UserIDLink2 to UserID and FriendID or similar - in which case you'd have up to double the records too.

                Also for the bi-directional configuration (A is a friend of B if B is a friend of A) you should set up indexes on the Friends table for (UserIDLink1,UserIDLink2) and (UserIDLink2,UserIDLink1) to ensure access is always efficient if we were searching either for friends of joe or friends of jane (if you didn't set up the second index then the first query would be an efficient index lookup but the second would require a full table scan).

                If your links were not bidirectional this wouldn't be necessary to find out who A's friends are, but you would still probably most require it as you'll likely also need to find out who B is a friend of.

                这篇关于MySQL好友表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Import CSV to Update rows in table(导入 CSV 以更新表中的行)
                Importing MaxMind#39;s GeoLite2 to MySQL(将 MaxMind 的 GeoLite2 导入 MySQL)
                How to save a Google maps overlay shape in the database?(如何在数据库中保存谷歌地图叠加形状?)
                Is there a performance hit using decimal data types (MySQL / Postgres)(使用十进制数据类型(MySQL/Postgres)是否会影响性能)
                MySQL truncates concatenated result of a GROUP_CONCAT function(MySQL 截断 GROUP_CONCAT 函数的连接结果)
                What#39;s the difference between VARCHAR(255) and TINYTEXT string types in MySQL?(MySQL 中的 VARCHAR(255) 和 TINYTEXT 字符串类型有什么区别?)

                <small id='U0qwG'></small><noframes id='U0qwG'>

                      • <bdo id='U0qwG'></bdo><ul id='U0qwG'></ul>
                      • <legend id='U0qwG'><style id='U0qwG'><dir id='U0qwG'><q id='U0qwG'></q></dir></style></legend>
                          <i id='U0qwG'><tr id='U0qwG'><dt id='U0qwG'><q id='U0qwG'><span id='U0qwG'><b id='U0qwG'><form id='U0qwG'><ins id='U0qwG'></ins><ul id='U0qwG'></ul><sub id='U0qwG'></sub></form><legend id='U0qwG'></legend><bdo id='U0qwG'><pre id='U0qwG'><center id='U0qwG'></center></pre></bdo></b><th id='U0qwG'></th></span></q></dt></tr></i><div id='U0qwG'><tfoot id='U0qwG'></tfoot><dl id='U0qwG'><fieldset id='U0qwG'></fieldset></dl></div>
                          <tfoot id='U0qwG'></tfoot>
                            <tbody id='U0qwG'></tbody>