多个外键引用其他表中的单个列 [英] multiple foreign keys referencing single column in other table

查看:34
本文介绍了多个外键引用其他表中的单个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能有 2 列引用不同表中的单个列.

I was wondering if it's possible to have 2 columns referencing a single column in a different table.

我有 2 个表 usersmatches

I have 2 tables users and matches

users 表包含 user_id 列中的所有用户 ID,匹配项包含两列 userAuserB.如果可能的话,将两列都引用到 users 表中的同一列是否明智?

The users table contains all the user id's in the column user_id and the matches contains two columns userA and userB. Would it be wise, if possible at all, to reference both columns to the same column in the users table?

我想使用它的原因是因为有人告诉我这是最佳实践并且它允许 DBMS 优化查询.

The reason why I want to use it, is because I've been told that it's best practice and that it allows the DBMS to optimize queries.

进一步解释一下,matches 表保存了选择了 userBuserA 的 id.如果 userB 也选择了 userA,则匹配.归结为约会应用.

To explain a bit further, the matches table holds the id's of userA who has selected userB. If userB also selects userA then there is a match. Boils down to a dating app.

推荐答案

是的,你可以做到:userA 来自指向用户的 user_id 的匹配项,以及 userB 来自用户的指向 user_id 的匹配项.这是有效的.

Yes, you can do it: userA from matches pointing to user_id from users, and also userB from matches pointing to user_id from users. This is valid.

这篇关于多个外键引用其他表中的单个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆