MySql数据库设计

发布于 2025-01-05 22:53:49 字数 260 浏览 1 评论 0原文

locations 包含 locIDlat &某个位置的名称值。

categories包含IDcategory-name & locID

如何将类别分配给多个位置?

多次存储同一类别(每个 locID 一次)是没有意义的。

Table locations contains locID, lat & long & name values of a certain location.

Table categories contains ID, category-name & locID.

How do I make categories assignable to multiple locations?

It doesn't make sense to have the same category stored multiple times, once for every locID.

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

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

发布评论

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

评论(1

孤独难免 2025-01-12 22:53:49

您可以使用联接表来建模多对多关系。

location_category
location_id   category_id
1             1
1             2
2             1
2             3
3             4

添加外键约束以防止在表中输入无效值:

  • location_category.location_idlocations.locID
  • location_category.category_id 到 < code>categories.ID

还要将 (location_id,category_id) 设置为表的主键,以防止多次将类别添加到同一位置。


从表中读取数据时,使用JOIN从主表中获取相关数据:

SELECT ...
FROM location_category
JOIN locations ON location_category.location_id = locations.locID
JOIN categories ON location_category.category_id = categories.ID
WHERE ....

You can use a join table to model the many-to-many relationship.

location_category
location_id   category_id
1             1
1             2
2             1
2             3
3             4

Add foreign key constraints to prevent invalid values from being entered into the table:

  • From location_category.location_id to locations.locID
  • From location_category.category_id to categories.ID

Also make (location_id, category_id) the primary key for the table to prevent adding a category to the same location multiple times.


When reading the data from the table, use JOINs to get the related data from the main tables:

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