表中的继承 - 结构问题
我的系统中有 3 种类型的用户。每种类型都有不同的信息 我创建了以下表:
BaseUser(base_user_id, username, password, additional common data)
base_user_id 是 PK,身份
UserType1(user_id, data related to type1 only)
user_id 是 PK 和 FK 到 base_user_id
UserType2(user_id, data related to type2 only)
user_id 是 PK 和 FK 到 base_user_id
UserType3(user_id, data related to type3 only)
user_id 是 PK 和 FK 到 base_user_id
现在我有了每种类型的用户与仓库表的关系。 type1 和 type2 的用户应仅具有warehouse_id,type3 的用户应具有warehouse_id 和customer_id。
我考虑了这个结构:
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
问题是这样的结构允许我想防止的两件事: 1. 将 type2 或 type1 的用户数据添加到 WarehouseOfTyp3User 中。 2. 将 type3 中的用户数据添加到 WarehouseOfUser 中。
这种情况的最佳结构是什么?
更新:我必须对所有用户类型使用相同的 id 范围。我无法为每个用户创建单独的表,因为我需要为每个表使用相同的 user_id 。
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_id, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是第二个答案,但有很大不同。我建议这样重组你的数据:
This is the second answer but it's very different. I suggest restructuring your data this way:
对于 user3 来说,解决这个问题似乎很容易 - 只需向 user3 表添加一个新的身份字段,从Warehouse3 表中删除 base_user_id,添加 user3Id 并使用 user3id 添加 user3 表的外键。所以没有baseUser表引用。这解决了问题1。
同样,将user1id和user2id添加到其他用户表中,从仓库表中删除base_user_id,仅添加userid并添加2个外键来链接到user1和user2表。这应该可以解决问题 2。
或者,您可以尝试使用触发器,但我不太喜欢触发器。
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.