首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RDBMS建模:具有极端历史版本的许多到多个

问RDBMS建模:具有极端历史版本的许多到多个
EN

Stack Overflow用户
提问于 2016-02-14 23:27:31
回答 1查看 59关注 0票数 3

您有两个表,foo和bar,它们具有M:N关系。

您希望维护foo和bar的相当极端的历史版本,以及它们之间的关系,这样:

  1. 您将一行插入到Foo中,然后将一行插入到Bar中,然后将一行插入到将两者连接在一起的FooBar中。您应该能够回顾过去,并看到Foo中的行与Bar中的行曾经是独立的。
  2. 然后将另一行插入到Bar中,并将一行插入到FooBar中,将第二个bar链接到第一个foo。您应该能够回顾过去,并看到foo行仅链接到第一个bar行。
  3. 然后更新foo行的一个属性。您应该能够回顾过去,并看到Bar中的两个行都曾经链接到具有前一个属性的foo行。

当我能够实现这个时,我的解决方案相当繁琐,并且为单个更新/插入带来了大量的DML操作。在Bar和Baz之间添加一个带有M:N的Baz表将显着地增加DMLs的数量。有标准的方法比下面的方法更好地完成这个任务吗?

这是我的解决方案:

DDL

代码语言:javascript
复制
Foo
--------------
foo_id            INT            --sequence generated
foo_version_id    INT     UNIQUE --sequence generated
foo_name          VARCHAR
active            INT     CHECK (active in (0,1))
CONSTRAINT  PRIMARY_KEY (foo_id, foo_version_id)


Bar
--------------
bar_id            INT            --sequence generated
bar_version_id    INT     UNIQUE --sequence generated
bar_name          VARCHAR
active            INT     CHECK (active in (0,1))
CONSTRAINT  PRIMARY_KEY (bar_id, bar_version_id)

FooBar
--------------
foo_version_id    FK to foo.foo_version_id
bar_version_id    FK to bar.bar_version_id
CONSTRAINT PRIMARY KEY (foo_version_id, bar_version_id)

DML

下面是这三种情况中每一种的伪代码。我已将这些作为程序加以实施。

对于case #1,这将导致4个DML操作将两个独立的foo和bar连接在一起,但不包括前两行:

代码语言:javascript
复制
Insert the first foo row
Insert the first bar row
Update the first foo row and set active to 0. 
Insert a new foo row with the same foo_id, foo_name, but new foo_version_id
Update the first bar row and set active to 0
Insert a new bar row and with the same bar_id, bar_name, but new bar_version_id
Insert a row into foo_bar with the foo_version_id and bar_version_id from the newly created active foo and bar rows.

对于case #2,这将导致9个DML操作将一个新的bar链接到链接到第一个bar的foo,而不包括第一行:

代码语言:javascript
复制
Insert the second bar row 
Update the active foo and set active to 0
Insert a new foo row with same foo_id, foo_name, but new foo_version_id
Update the first active bar and set active to 0
Insert a new bar row with same bar_id, bar_name, but new bar_version_id
Update the second active bar and set active to 0
Insert a new bar row with same bar_id, bar_name, but new bar_version_id
Insert a row into foo_bar with the foo_version_id and bar_version_id from the foo and first bar.
Insert a row into foo_bar with the foo_version_id and bar_version_id from the foo and second bar.

对于case #3,这将导致8个DML操作来更新链接到两个bars的foo上的一个属性。

代码语言:javascript
复制
Update the active foo and set active to 0
Insert a new foo row with same foo_id, but new foo_version_id, foo_name
(repeat from case #2 starting at line 4)

SQL

给定一个已知的foo_id,我可以离开foo、foo_bar、bar on foo_version_id和bar_version_id,查看所讨论的特定foo的所有可能的历史状态。

代码语言:javascript
复制
select f.foo_id, f.foo_version_id, f.foo_name, b.bar_id, b.bar_version_id, b.bar_name
FROM foo f, foo_bar fb, bar b
WHERE 1 = 1
    AND f.foo_version_id = fb.foo_version_id (+)
    AND fb.bar_version_id = b.bar_version_id (+)
ORDER BY f.foo_version_id, b.bar_version_id
;

foo_id | foo_version_id | foo_name | bar_id | bar_version_id | bar_name
     1 |              1 |        a |        |                |           -- 1) independent foo
     1 |              2 |        a |      1 |              2 |       b   -- 2) link foo to first bar
     1 |              3 |        a |      1 |              4 |       b   -- 3) link second bar to foo
     1 |              3 |        a |      2 |              5 |       b2  -- 3) link second bar to foo
     1 |              4 |        A |      1 |              6 |       b   -- 4) rename foo_name to A
     1 |              4 |        A |      2 |              7 |       b2  -- 4) rename foo_name to A
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-15 17:54:44

您需要一个时态数据库(支持SQL:2011时态(或类似的专有系统))

据我所知,没有任何开源数据库支持这一点。我已经缠着波斯格斯一家一段时间了。

这意味着你需要花点钱。以下DBs支持它:

IBM DB2 10+

Oracle 12c

MS SQL Server 2016

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35399312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档