跨多个数据库的 SQLite 视图。这样可以吗?有更好的办法吗?
使用 SQlite 我有一个大型数据库,分为几年: DB_2006_thru_2007.sq3 DB_2008_thru_2009.sq3 DB_current.sq3 它们都有一个名为 hist_tbl 的表,其中包含两列(键、数据)。
要求是: 1.能够同时访问所有数据。 2. 插入仅转到当前版本。 3.随着时间的推移,数据会不断分裂。 4. 访问是通过具有独占访问权的单个程序进行的。 5. 程序可以接受一些设置SQL,但在访问一个或多个数据库时需要运行相同的SQL。
为了连贯地查看它们,我执行以下操作(实际上是在程序中,但此处显示命令行): sqlite3 DB_current.sq3
将数据库“DB_2006_thru_2007.sq3”附加为 hist1; 将数据库“DB_2008_thru_2009.sq3”附加为 hist2; 创建临时视图 hist_tbl 作为 从 hist1.hist_tbl 联合中选择 * 从 hist2.hist_tbl 联合中选择 * 从 main.hist_tbl 中选择*;
现在有一个 temp.hist_tbl (视图)和一个 main.hist_tbl (表)。 当我选择而不限定表时,我通过视图获取数据。 这是可取的,因为我可以根据我的设置方式对连接视图或单个数据库使用预设的 SQL 查询。另外,我总是可以插入到 main.hist_tbl 中。
问题1:有什么缺点? 问题2:有更好的办法吗?
提前致谢。
Using SQlite I have a large database split into years:
DB_2006_thru_2007.sq3
DB_2008_thru_2009.sq3
DB_current.sq3
They all have a single table call hist_tbl with two columns (key, data).
The requirements are:
1. to be able to access all the data at once.
2. inserts only go to the current version.
3. the data will continue to be split as time goes on.
4. access is through a single program that has exclusive access.
5. the program can accept some setup SQL but needs to run the same when accessing one database or multiple databases.
To view them cohesively I do the following (really in a program but command line shown here):
sqlite3 DB_current.sq3
attach database 'DB_2006_thru_2007.sq3' as hist1;
attach database 'DB_2008_thru_2009.sq3' as hist2;
create temp view hist_tbl as
select * from hist1.hist_tbl union
select * from hist2.hist_tbl union
select * from main.hist_tbl;
There is now a temp.hist_tbl (view) and a main.hist_tbl (table).
When I select without qualifying the table I get the data thru the view.
This is desirable since I can use my canned sql queries against either the joined view or the individual databases depending on how I setup. Additionally I can always insert into main.hist_tbl.
Question 1: What are the downsides?
Question 2: Is there a better way?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题1:有什么缺点?
您必须每次更新视图。 财政。年。
问题2:有更好的办法吗?
添加日期列,以便您可以搜索给定时间范围(例如会计年度)内的内容。
Question 1: What are the downsides?
You have to update the view EVERY. FISCAL. year.
Question 2: Is there a better way?
Add a date column so you can search for things within a given timespan, like a fiscal year.