且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据库模型结构

更新时间:2023-02-02 21:53:08

group_types 存储为一个非统计表(具有嵌套集 parent-child 模型):

Store your group_types as a hieararchical table (with nested sets or parent-child model):

父子


typeid parent name

1      0      Buyers
2      0      Sellers
3      0      Referee
4      1      Electrical
5      1      Mechanic



SELECT  *
FROM    mytable
WHERE   group IN
        (
        SELECT  typeid
        FROM    group_types
        START WITH
                typeid = 1
        CONNECT BY
                parent = PRIOR typeid
        )

将选择 Oracle

嵌套集


typeid lower  upper  Name
1      1      2      Buyers
2      3      3      Sellers
3      4      4      Referee
4      1      1      Electrical
5      2      2      Mechanic



SELECT  *
FROM    group_types
JOIN    mytable
ON      group BETWEEN lower AND upper
WHERE   typeid = 1

将选择任何数据库中的所有买方。

will select all buyers in any database.

嵌套集可在任何地方实施,需要对 group_types 进行分层排序或频繁更新。

Nested sets is implementable anywhere and more performant, if you don't need hierarchical ordering or frequent updates on group_types.

父子可以在 Oracle SQL Server 中轻松实现,并在

Parent-child is implementable easily in Oracle and SQL Server and with a little effort in MySQL. It allow easy structure changing and hierarchical ordering.

请参阅我的博客中关于如何在 MySQL中实现它的文章

See this article in my blog on how to implement it in MySQL:

  • Hierarchical queries in MySQL