我正在使用事务数据库:一个关系数据库,其中表中的一行被定义为数据库在给定时间“认为”该行的字段包含的一组值。
部分架构:
CREATE TABLE Article
(
id INTEGER,
PRIMARY KEY(id)
);
CREATE TABLE Article_headline
(
Article_id INTEGER,
headline TEXT,
tt DATETIME
FOREIGN KEY(Article_id) REFERENCES Article(id)
);
CREATE TABLE Article_body
(
Article_id INTEGER,
body TEXT,
tt DATETIME
FOREIGN KEY(Article_id) REFERENCES Article(id)
);
换句话说,虚拟“文章”表有三列:id、标题和正文。
我可以创建临时视图来表示由给定时间定义的虚拟“文章”表:
CREATE TEMPORARY VIEW Article_headline_old
AS SELECT Article_id, headline, MAX(tt)
FROM Article_headline
WHERE tt <= "2011-11-03 16:05:23"
GROUP BY Article_id;
CREATE TEMPORARY VIEW Article_body_old
AS SELECT Article_id, body, MAX(tt)
FROM Article_body
WHERE tt <= "2011-11-03 16:05:23"
GROUP BY Article_id;
CREATE TEMPORARY VIEW Article_old
AS SELECT id, headline, body
FROM Article, Article_headline_old, Article_body_old
WHERE Article_headline_old.Article_id = Article.id
AND Article_body_old.Article_id = Article.id;
每次我想查询数据库获取文章时,我都必须创建这三个临时视图,我宁愿不这样做。我更愿意创建永久视图“函数”作为架构本身的一部分:
CREATE VIEW Article_headline_at(theTime)
AS SELECT Article_id, headline, MAX(tt)
FROM Article_headline
WHERE tt <= theTime
GROUP BY Article_id;
CREATE VIEW Article_body_at(theTime)
AS SELECT Article_id, body, MAX(tt)
FROM Article_body
WHERE tt <= theTime
GROUP BY Article_id;
CREATE VIEW Article_at(theTime)
AS SELECT id, headline, body
FROM Article,
Article_headline_at(theTime) AS Article_headline_old,
Article_body_at(theTime) AS Article_body_old
WHERE Article_headline_old.Article_id = Article.id
AND Article_body_old.Article_id = Article.id;
然后当我想从数据库中选择一篇文章时:
SELECT * FROM Article_at("2011-11-03 16:05:23");
SQL 有没有类似的功能?