SQL 条件关系

发布于 2024-11-27 19:04:55 字数 1911 浏览 0 评论 0原文

我针对同一想法提出了两种方法,并且希望通过使用其中一种来避免任何明显的陷阱。我有一个表(tbl_post),其中单行可以与其他表(tbl_category、tbl_site、tbl_team)有许多关系。我有一个关系表来连接这些表,但不知道要使用哪种结构,是有条件的还是直接的?希望下面的内容能够解释...

tbl_post (simple post, can be associated with many categories, teams and sites)
* id
* title
* content

tbl_category
* id
* title
* other category only columns

tbl_team
* id
* title
* other team only columns

tbl_site
* id
* title
* other site only columns

----------------------------------------------------------
tbl_post_relationship
* id (pk)
* post_id (fk tbl_post)
* related_id (fk, dependant on related_type to either tbl_category, tbl_site or tbl_team)
* related_type (category, site or team)

____________________________________
|id|post_id|related_id|related_type|
|--|-------|----------|------------|
| 1|      1|         6|    category|
| 2|      1|         4|        site|
| 3|      1|         9|    category|
| 4|      1|         3|        team|
------------------------------------

SELECT  c.*
FROM    tbl_category c
        JOIN tbl_relationship r ON
            r.post_id = 1
            AND r.related_type = 'category'
            AND c.id = r.related_id

------------- OR ---------------

tbl_post_relationship
* id (pk)
* post_id (fk tbl_post)
* category_id (fk tbl_category)
* site_id (fk tbl_site)
* team_id (fk tbl_team)

________________________________________
|id|post_id|category_id|site_id|team_id|
|--|-------|-----------|-------|-------|
| 1|      1|          6|   NULL|   NULL|
| 2|      1|       NULL|      4|   NULL|
| 3|      1|          9|   NULL|   NULL|
| 4|      1|       NULL|   NULL|      3|
----------------------------------------

SELECT  c.*
FROM    tbl_category c
        JOIN tbl_relationship r ON
            r.post_id = 1
            AND r.category_id = c.id

因此,使用一种方法,我最终会得到很多带有 NULL 的列(可能有更多的表)。或者我最终得到一个简单的表来维护它,但每个连接都基于“类型”。我也知道每个关系可以有一个表,但同样感觉表太多了。有什么想法/想法吗?

I've come up with two approaches to the same idea and would like to avoid any obvious pitfalls by using one over the other. I have a table (tbl_post) where a single row can have many relationships to other tables (tbl_category, tbl_site, tbl_team). I have a relationship table to join these but don't know which structure to go with, conditional or direct? Hopefully the following will explain...

tbl_post (simple post, can be associated with many categories, teams and sites)
* id
* title
* content

tbl_category
* id
* title
* other category only columns

tbl_team
* id
* title
* other team only columns

tbl_site
* id
* title
* other site only columns

----------------------------------------------------------
tbl_post_relationship
* id (pk)
* post_id (fk tbl_post)
* related_id (fk, dependant on related_type to either tbl_category, tbl_site or tbl_team)
* related_type (category, site or team)

____________________________________
|id|post_id|related_id|related_type|
|--|-------|----------|------------|
| 1|      1|         6|    category|
| 2|      1|         4|        site|
| 3|      1|         9|    category|
| 4|      1|         3|        team|
------------------------------------

SELECT  c.*
FROM    tbl_category c
        JOIN tbl_relationship r ON
            r.post_id = 1
            AND r.related_type = 'category'
            AND c.id = r.related_id

------------- OR ---------------

tbl_post_relationship
* id (pk)
* post_id (fk tbl_post)
* category_id (fk tbl_category)
* site_id (fk tbl_site)
* team_id (fk tbl_team)

________________________________________
|id|post_id|category_id|site_id|team_id|
|--|-------|-----------|-------|-------|
| 1|      1|          6|   NULL|   NULL|
| 2|      1|       NULL|      4|   NULL|
| 3|      1|          9|   NULL|   NULL|
| 4|      1|       NULL|   NULL|      3|
----------------------------------------

SELECT  c.*
FROM    tbl_category c
        JOIN tbl_relationship r ON
            r.post_id = 1
            AND r.category_id = c.id

So with the one approach I'll end up with lots of columns (there might be more tables) with NULL's. Or I end up with one simple table to maintain it, but every join is based on a "type". I also know I could have a table per relationship, but again that feels like too many tables. Any ideas / thoughts?

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

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

发布评论

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

评论(1

何处潇湘 2024-12-04 19:04:55

每个关系最好使用一张表。您不必担心桌子的数量。单个关系表的缺点有很多,而且风险很大:

1)如果相关表的行与行不同,则无法强制执行外键,因此您的数据完整性面临风险……迟早您将拥有孤立数据。

2)查询更加复杂,因为你必须使用 related_type 来过滤掉很多地方的关系。

3) 查询维护成本更高,原因与 2) 相同,并且因为您必须在许多地方显式使用 related_type 常量...当您需要更改它们或添加一些常量时,这将是地狱。

我建议你使用正统的设计……只有 3 个不同的关系表:post_category、post_team、post_site。

You are best out with one table per relationship. You should not worry about the amount of tables. The drawbacks of a single relationship table are several, and quite risky:

1) You cannot enforce foreign keys if the related tables vary from row to row, so your data integrity is at risk... and sooner or later you will have orphaned data.

2) Queries are more complex because you have to use the related_type to filter out the relations in many places.

3) Query maintenance is more costly, for the same reasons of 2), and because you have to explicitly use the related_type constants in many places... it'll be hell when you need to change them or add some.

I'd suggest you use the orthodox design... just got with 3 distinct relationship tables: post_category, post_team, post_site.

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