在SQLlite数据库中每每一个数据文件就是一个schema,可是在平时的业务或者是一些条件中多是不一样的内容存放在不一样的schema中,即不一样的数据文件,有的场景下须要数据关联时就能够使用SQLlite的数据附加来创建一个临时的连接。以下,在使用my_test的schema时须要关联查询一个为my_test2的schema就能够使用附加:sql
[root@localhost data]# sqlite3 my_test.db #在SQLlite数据库中缺省database名为main SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .database seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db sqlite> ATTACH DATABASE '/data/my_test2.db' As 'my_test2'; #在当前schema下附加上/data/my_test2.db中的数据,而且起一个别名为my_test2,固然也能够起其余的名字 sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db 2 my_test2 /data/my_test2.db sqlite> CREATE TABLE my_test2.test_attach ( ...> a int(10), ...> b int(10) ...> ); sqlite> SELECT * FROM my_test2.sqlite_master WHERE type = 'table' AND tbl_name = 'test_attach'; #直接在当前schema下使用/data/my_test2.db中的数据,而且查看 table|test_attach|test_attach|4|CREATE TABLE test_attach ( a int(10), b int(10) ) sqlite> .exit [root@localhost data]# sqlite3 /data/my_test2.db #切换成my_test2.db的schema查看验证下 SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'test_attach'; CREATE TABLE test_attach ( a int(10), b int(10) )
如此就是在SQLlite数据库中的附加数据库,它实际上是一个连接,用于在不一样的数据schma数据文件下使用其余的schma数据文件,在这里须要注意的是目前在SQLlite数据库中附加是临时的,在当前session中建立一个连接,若是在退出这个session后附加就自动分离:数据库
[root@localhost data]# sqlite3 /data/my_test.db SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .database seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db
固然有若是有附件数据库那必定有分离,分离就比较简单:bash
sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db 2 my_test2 /data/my_test2.db sqlite> DETACH DATABASE "my_test2"; sqlite> .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db
这样就成功的主动分离附加在当前schma下的其余数据文件,在这里要特别注意的是若是分离的数据库是在内存或临时空间内,分离后会销毁其分离的数据session