如何在 SQLite 中合并多个数据库文件?

发布于 2024-10-16 03:02:16 字数 426 浏览 3 评论 0原文

我有多个数据库文件,它们存在于多个位置,具有完全相同的结构。我知道附加功能可用于将多个文件连接到一个数据库连接,但是,这会将它们视为单独的数据库。我想做类似的事情:

SELECT uid, name FROM ALL_DATABASES.Users;

另外,

SELECT uid, name FROM DB1.Users UNION SELECT uid, name FROM DB2.Users ;

这不是一个有效的答案,因为我有任意数量的数据库文件需要合并。最后,数据库文件必须保持独立。有人知道如何做到这一点吗?

编辑:一个答案给了我一个想法:是否可以创建一个由所有不同表组合而成的视图?是否可以查询所有数据库文件以及它们“安装”的数据库,然后在视图查询中使用它来创建“主表”?

I have multiple database files which exist in multiple locations with exactly similar structure. I understand the attach function can be used to connect multiple files to one database connection, however, this treats them as seperate databases. I want to do something like:

SELECT uid, name FROM ALL_DATABASES.Users;

Also,

SELECT uid, name FROM DB1.Users UNION SELECT uid, name FROM DB2.Users ;

is NOT a valid answer because I have an arbitrary number of database files that I need to merge. Lastly, the database files, must stay seperate. anyone know how to accomplish this?

EDIT: an answer gave me the idea: would it be possible to create a view which is a combination of all the different tables? Is it possible to query for all database files and which databases they 'mount' and then use that inside the view query to create the 'master table'?

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

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

发布评论

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

评论(2

哎呦我呸! 2024-10-23 03:02:16

由于 SQLite 对一次可以附加的数据库数量施加了限制,因此无法在单个查询中完成您想要的操作。

如果可以保证该数量在 SQLite 的限制范围内(这违反了“任意”的定义),那么就没有什么可以阻止您在需要时使用正确的 UNION 集生成查询来执行它。

要支持真正任意数量的表,唯一真正的选择是在不相关的数据库中创建一个表,并从每个候选中重复 INSERT 行:

ATTACH DATABASE '/path/to/candidate/database' AS candidate;
INSERT INTO some_table (uid, name) SELECT uid, name FROM candidate.User;
DETACH DATABASE candidate;

Because SQLite imposes a limit on the number of databases that can be attached at one time, there is no way to do what you want in a single query.

If the number can be guaranteed to be within SQLite's limit (which violates the definition of "arbitrary"), there's nothing that prevents you from generating a query with the right set of UNIONs at the time you need to execute it.

To support truly arbitrary numbers of tables, your only real option is to create a table in an unrelated database and repeatedly INSERT rows from each candidate:

ATTACH DATABASE '/path/to/candidate/database' AS candidate;
INSERT INTO some_table (uid, name) SELECT uid, name FROM candidate.User;
DETACH DATABASE candidate;
空城之時有危險 2024-10-23 03:02:16

模式中的一些聪明之处可以解决这个问题。

您通常有两种类型的表:参考表和动态表。
参考表在所有数据库中具有相同的内容,例如国家代码、部门代码等。

动态数据是每个数据库唯一的数据,例如时间序列、销售统计数据等。

参考数据应保存在主数据库中,并在更改后复制到动态数据库。

动态表都应该有一列 DB_ID,它是复合主键的一部分,例如您的时间序列可能使用 db_id、measurement_id、time_stamp。您还可以使用 DB_ID 上的哈希来生成主键,对数据库中的所有表使用相同的 pk 生成器。当合并来自不同 DBS 的这些数据时,数据将是唯一的。

因此,您将拥有 3 种类型的数据库:

  • Reference master ->复制给所有其他人

  • 个人动态->复制到完整动态

  • 全动态 ->复制自参考母版和所有个人动态。

然后,由您决定如何进行这种复制、伪实时或强力复制、每天或根据需要截断和重建完整的动态。

Some cleverness in the schema would take care of this.

You will generally have 2 types of tables: reference tables, and dynamic tables.
Reference tables have the same content across all databases, for example country codes, department codes, etc.

Dynamic data is data that will be unique to each DB, for example time series, sales statistics,etc.

The reference data should be maintained in a master DB, and replicated to the dynamic databases after changes.

The dynamic tables should all have a column for DB_ID, which would be part of a compound primary key, for example your time series might use db_id,measurement_id,time_stamp. You could also use a hash on DB_ID to generate primary keys, use same pk generator for all tables in DB. When merging these from different DBS , the data will be unique.

So you will have 3 types of databases:

  • Reference master -> replicated to all others

  • individual dynamic -> replicated to full dynamic

  • full dynamic -> replicated from reference master and all individual dynamic.

Then, it is up to you how you will do this replication, pseudo-realtime or brute force, truncate and rebuild the full dynamic every day or as needed.

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