选择哪种数据库结构? [英] Which database structure to choose?
问题描述
我想实施一个通知系统.我有用户,每个用户都有通知设置
I want to implement a notification system. I have users and each user has notification setting
结构1:
Users Notification_settings Notifications
-id (pk) -id -id (pk)
-username -user_id (fk) references Users -user_id (fk)
-password -receive_email (boolean) -message
-is_read
结构2:
Users Notifications_settings
-id (pk) -id (pk)
-setting_id (fk) references Notifications_settings -receive_email
-username
-password
Notifications
-id (pk)
-user_id (fk)
-message
-is_read
要选择哪种数据库结构或用于通知系统的任何其他数据库结构?
Which database structure to choose or any other database structure for notification system ?
推荐答案
似乎应该是用户<通知(一对多),但也许您有一个特定的理由将其设置为1-1.在那种情况下,我将使父表(没有FK列的父表)具有更多的一对多关系.因此,自然地,将用户ID存储在通知表中是很有意义的.
It seems like it should be users < notifications (one to many), but maybe you have a specific reason that it should be 1-1. In that case, I would make the parent table (the one without the FK column) the table with more one-to-many relationships. So, naturally, it would make sense to use store the user ID in the notification table.
似乎通知将总是有一个用户,但反之亦然.因此,您应该将外键存储到通知表中的UserID上,而不是相反.
It would seem that a notification would always have a user, but NOT vice-versa. Therefore, you should store the foreign key to a UserID in your notification table -- and not the other way around.
编辑-正如其他人所建议的那样,如果您确实确实想要1-1关系,则可以仅在User表中添加通知字段.这些似乎一眼就违反了规范化规则,但是如果它是一对一关系,那么一定要做到这一点
编辑2-由于您明确声明没有用户就不存在通知,因此我将明确地说您应将外键存储到Users的Notifications表中,没有例外(除非您希望将通知信息存储在用户表中:)
编辑#2937:
您应该将用户的通知首选项存储在用户表中-除非您进行了一些混淆的设计并且有256列用于您的用户已经是限制了.
You should store the user's notification preferences in the User table - there's no need to split that into a different table unless you have some obfuscated design and have 256 columns for your user already and that is the limit.
您应该将通知存储在分开的表中,并且用户与通知之间存在一对多的关系.那是我最后的回答,瑞吉斯:)
You should store the notifications in a separate table, with a One-Many Relationship from Users to Notifications. That is my final answe, Regis :)
这篇关于选择哪种数据库结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!