在SQLlite数据库中,一个数据文件往往是一个schema,但是在正常的业务或者某些情况下,不同的内容可能存储在不同的schema中,也就是不同的数据文件,在某些场景下,当需要数据关联,可以使用SQLlite数据附件建立临时链接。如下,在使用my_test的schema时,需要查询一个my_test2的schema才能使用添加:[root@localhostdata]#sqlite3my_test.db#SQLlite数据库中默认的数据库名称是mainSQLiteversion3.6.20输入".help"forinstructionsEnterSQLstatementsterminatedwitha";"sqlite>.databaseseqnamefile----------------------------------------------------------------------0main/data/my_test.dbsqlite>ATTACHDATABASE'/data/my_test2.db'As'my_test2';#在当前schema下添加/data/my_test2.db中的数据,并给它取一个别名my_test2,当然你也可以给它起另一个名字sqlite>.databasesseqnamefile------------------------------------------------------------------------0main/data/my_test.db2my_test2/data/my_test2.dbsqlite>创建表my_test2.test_attach(...>aint(10),...>bint(10)...>);sqlite>SELECT*FROMmy_test2.sqlite_masterWHEREtype='table'ANDtbl_name='test_attach';#在当前schema下直接使用/data/my_test2.db中的数据,查看table|test_attach|test_attach|4|创建表test_attach(aint(10),bint(10))sqlite>.exit[root@localhostdata]#sqlite3/data/my_test2.db#切换到my_test2.db的schema查看验证SQLiteversion3.6.20Enter".help"forinstructionsEnterSQLstatementsterminatedwitha";"sqlite>SELECTsqlFROMsqlite_masterWHEREtype='table'ANDtbl_name='test_attach';CREATETABLEtest_attach(aint(10),bint(10))这是SQLlite数据库中的附加数据库。它实际上是在不同的dataschma数据文件下使用其他schma数据文件的链接。这里需要注意的是,在SQLlite数据库中的添加是临时的。在当前会话中创建链接。如果退出session后附加,会自动分离:[root@localhostdata]#sqlite3/data/my_test.dbSQLiteversion3.6.20Enter".help"forinstructionsEnterSQLstatementsterminatedwitha";"sqlite>.databaseseqnamefile-------------------------------------------------------------------------0main/data/my_test.db当然,如果有附件数据库,就一定要有分离,分离比较简单:sqlite>.databasesseqnamefile--------------------------------------------------------------------0main/data/my_test.db2my_test2/data/my_test2.dbsqlite>DETACHDATABASE"my_test2";sqlite>.databasesseqnamefile--------------------------------------------------------------------------0main/data/my_test.db成功主动分离并附加到For当前schma下的其他数据文件,这里要特别注意,如果分离出来的数据库在内存或者临时空间中,分离后的数据会被销毁
