表中的继承 - 结构问题

发布于 2024-10-11 22:16:24 字数 1111 浏览 2 评论 0原文

我的系统中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-10-18 22:16:24

这是第二个答案,但有很大不同。我建议这样重组你的数据:

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...)

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...)
动次打次papapa 2024-10-18 22:16:24

对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文