使用 MySQL 进行关系数据库设计

发布于 2024-11-05 14:19:01 字数 769 浏览 0 评论 0原文

我目前有 3 张桌子

Users (Id, PositionId)
MonsterInstances (Id, PositionId)
TreasureInstances (Id, PositionId)

和 1 个位置表。

Positions (Id, Coordinate, TypeId)

在我的 3 个表中,PositionId 是我的 Position 表的外键。

我想使用单个持仓表(如上所示)来标准化我的所有持仓数据。我面临的问题是我必须识别一种类型,以便当我的查询执行时,它知道要查询哪个表。

例如,

SP  -- GetObjectByPosition (positionId)

IF TypeId = 1
SELECT * FROM Users JOIN... WHERE PositionId = positionId
ELSE IF TypeId = 2
SELECT * FROM MonsterInstances JOIN...

这对我来说似乎是糟糕的设计。我能想到的唯一解决办法是有 3 个单独的桌子。

UserPositions
MonsterInstancePositions
TreasureInstancePositions

然而,我并不总是对提取用户、怪物或宝藏数据感兴趣。有时我只想要职位 ID 和位置——这意味着对于三个表,我必须进行联合。

有更好的方法吗?

I currently have 3 tables,

Users (Id, PositionId)
MonsterInstances (Id, PositionId)
TreasureInstances (Id, PositionId)

and 1 position table.

Positions (Id, Coordinate, TypeId)

PositionId, in my 3 tables, are foreign keys into my Position table.

I want to use a single Positions table, as shown above, to normalize all of my position data. The problem I am facing is that I must identify a type so that when my query executes, it knows which table to query.

e.g.

SP  -- GetObjectByPosition (positionId)

IF TypeId = 1
SELECT * FROM Users JOIN... WHERE PositionId = positionId
ELSE IF TypeId = 2
SELECT * FROM MonsterInstances JOIN...

This seems like bad design to me. The only way around it I can percieve would be to have 3 seperate tables.

UserPositions
MonsterInstancePositions
TreasureInstancePositions

However, I'm not always interested in extracting user, monster, or treasure data. Sometimes I only want the position Id and location -- which would mean with three tables, I would have to do a union.

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

川水往事 2024-11-12 14:19:01

用户、MonsterInstances、TreasureInstances 可以重写为包含类型列的单个“ObjectInstances”表。然后,分别针对这 3 个表执行的查询将针对 ObjectInstances 和 typeID 执行,并引用新的 OjbectTypes 表。有道理吗?

Users, MonsterInstances, TreasureInstances could be rewritten as a single "ObjectInstances" table that includes a type column. Then queries that would work against those 3 tables separately would instead work against ObjectInstances and a typeID, referencing a new OjbectTypes table. Make sense?

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