简体   繁体   中英

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

For example, I have three tables that store different types of users with different 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

您可以在Contact表中使用复合键,例如UserTypeID,UserId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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