简体   繁体   English

无法找出正确的数据库设计

[英]Unable to figure out correct database design

I am designing database for social networking site with "followers" and "following" feature like twitter. 我正在为社交网站设计具有Twitter的“关注者”和“关注者”功能的数据库。 There are some relations which are common to whole circle ie to whom i am following and to my followers too.. I have a table Following with uid1 and uid2. 整个圈子有一些共同点,即我关注的对象以及我的关注者。.我有一个表uid1和uid2。 Suppose A follows B and B follows A and C. It will have entries like 假设A跟随B,B跟随A和C。它将具有类似

uid1 uid2
A      B
B      A
B      C

Now i want a third field relationId in the same table which should be unique to a relation whether A follows B or B follows A like 现在我想要同一个表中的第三个字段的relationId,无论该关系是A跟随B还是B跟随A都应该是唯一的

uid1 uid2  relationId
A     B      1
B     A      1
B     C      2 

so that i can use relationId as foreign key in another table to store features common to whole circle of a user. 这样我就可以在另一个表中将RelationId用作外键来存储用户整个圈子所共有的功能。 One thing immediately comes to mind is if there can be a commutative formula to fetch a unique number( relationId) given two numbers(uid1 and uid2). 立即想到的是,如果给定两个数字(uid1和uid2),是否可以使用一个交换公式来获取唯一的number(relationId)。 But what it can be? 但这可能是什么?

Edit 编辑


I also have one more doubt. 我也有一个疑问。 Since username in my database is unique. 由于我的数据库中的用户名是唯一的。 So shall i use username as primary key in whole database or do we have a performance benefit by using a number as primary key as uid in my case with a table to resolve uid and username? 因此,我应该在整个数据库中使用用户名作为主键还是通过使用数字作为主键作为uid(在我的情况下使用表来解析uid和用户名)来提高性能?

I would do it similar to the way you have it: 我将按照您的方式进行操作:

Follows
-------
follower followed
A        B
B        A
B        C

This could obviously have information like followDate which makes sense in both directions. 显然,这可能具有诸如followDate信息,这在两个方向上都有意义。

Then you can also have: 然后您还可以拥有:

Connected
-----------------
relationId uid1 uid2
x          2    3

with relationId an artificial key (could be INT AUTOINCREMENT, GUID, etc.) 使用relationId人工键(可以是INT AUTOINCREMENT,GUID等)

However, Connected would have a constraint (whether you can enforce this in the db layer depends on your system) that uid1 < uid2 . 但是,Connected将具有uid1 < uid2的约束(是否可以在db层中强制执行此操作取决于您的系统)。 Obviously, this also implies you'll never have it both ways. 显然,这也意味着您永远都不会拥有这两种方式。

This is relatively simple, easy to query, and should have decent performance with a good composite unique index on uid1 and uid2 . 这是比较简单的,便于查询,并应具有上好的复合唯一索引不俗的表现uid1uid2 When searching for non-directional information about the relationship, you don't have to look at indices for both directions. 在搜索有关关系的非方向性信息时,您不必查看两个方向的索引。

EDIT: I would recommend using an artificial key for users. 编辑:我建议为用户使用人工密钥。 Even if you don't allow username changes now, you may want to do so later. 即使您现在不允许更改用户名,您也可能以后要这样做。

EDIT 2: According to this , you can reject an insert in a trigger by setting a NOT NULL column to NULL. 编辑2:根据这个 ,你可以拒绝在插入触发一个NOT NULL列设置为NULL。 Both uids should be NOT NULL, so you can just set one to NULL if uid1 >= uid2 . 两个uid均应为NOT NULL,因此,如果uid1 >= uid2 ,则只需将其设置为NULL uid1 >= uid2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM