开发者

What is the best database design for multiple tables with 1 to 1 relationship to the same table?

开发者 https://www.devze.com 2022-12-21 19:47 出处:网络
For example, I have three tables that store different types of users with differen开发者_如何转开发t data. But now I want to store their contact information. The table that stores their contact info w

For example, I have three tables that store different types of users with differen开发者_如何转开发t data. But now I want to store their contact information. The table that stores their contact info would be suitable for storing data for all of the user tables.

The problem is that I have different or possibly duplicating ID's between the different user tables so it makes it impossible to create a relationship to my contact table. One solution is to create a contact table for each user type, but it seems wasteful since the only difference would be the ID.

Also, I thought of storing the contact ID in the user table but this seems less then ideal as we may not have the users contact info until later, if at all.

Any other options that I'm missing?


This is what I'd do:

table Users
UserID                 -PK auto number
UserLogin
UserName

table UserSpecialType1
UserSpecialType1ID     -PK auto number
UserID                 -FK
SpecialInfoA
SpecialInfoB

table UserSpecialType2
UserSpecialType2ID     -PK auto number
UserID                 -FK
SpecialInfoC
SpecialInfoD

table UserContactInfo
UserContactInfoID      -PK auto number
UserID                 -FK
EmailAddress
PhoneNumber
Address


Off the top of my head, I would put all of the Users into the same table with a Type differentiator.

TABLE User
Id
Value1
Value2
UserTypeCode

TABLE UserType
TypeCode

TABLE Contact
UserId
ContactInfo

TABLE UserTypeAttribute
UserType
AttributeTypeCode

TABLE AttributeType
AttributeTypeCode

TABLE UserAttributeTypeValue
UserId
AttributeTypeCode
Value


You can use a compound key in the Contact table, e.g. UserTypeID, UserId

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号