开发者

Inheritance in tables - structure problem

I have 3 types of users in my system. each type has different information I created the following tables:

BaseUser(base_user_id, username, password, additional common data)

base_user_id is PK and Identity

UserType1(user_id, data related to type1 only)

user_id is PK and FK to base_user_id

UserType2(user_id, data related to type2 only)

user_id is PK and FK to base_user_id

UserType3(user_i开发者_开发技巧d, data related to type3 only)

user_id is PK and FK to base_user_id

Now I have relation from each type of user to warehouses table. Users from type1 and type2 should have only warehouse_id and users from type3 should have warehouse_id and customer_id.

I thought about this structure:

WarehouseOfUser(base_user_id,warehouse_id)

base_user_id is FK to base_user_id in BaseUser

WarehouseOfTyp3User(base_user_id,warehouse_id, customer_id)

base_user_id is FK to base_user_id in BaseUser

The problem is that such structure allows 2 things I want to prevent: 1. add to WarehouseOfTyp3User data of user from type2 or type1. 2. add to WarehouseOfUser data of user from type3.

what is the best structure for such case?

Update: I must use the same id's range for all users types. I cannot create seperate table for each user since I need the same user_id for each table.


This is the second answer but it's very different. I suggest restructuring your data this way:

table BaseUser(baseUserId, password,...)
table User1(user1Id -> PK, baseUserId -> FK to BaseUser, warehouse1id -> FK to warehouse1 table) 
table User2(user2Id -> PK, baseUserId -> FK to BaseUser, warehouse1id -> FK to warehouse1 table)
table User3(user3Id -> PK, baseUserId -> FK to BaseUser, warehouse3id -> FK to warehouse3 table, customerId)
table Warehouse1(warehouse1id...)
table Warehouse3(warehouse3id...)


It appears easy to solve for user3 - just add a new identity field to user3 table, remove base_user_id from warehouse3 table, add user3Id instead and add foreign key with user3 table using user3id. So no baseUser table reference. This solves problem 1.

Similarly, add user1id and user2id to other user tables, remove base_user_id from warehouse table, add just userid and add 2 foreign keys to link to user1 and user2 tables. This should solve problem 2.

Alternatively you may try to use triggers but I don't particularly like triggers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜