使用關系型資料庫時,經常需要在不同的表(Table)之間聯合操作。對于同一個資料庫中的表,容易實作,譬如使用JOIN。如果表處于不同的資料庫(.db檔案),如何實作?
SQLite以“連接配接”為使用資料庫的基本方式,一個連接配接對應到一個db檔案。對于連接配接到資料庫A,同時需要使用資料庫B中的資料的情況,SQLite提供了一種将外部資料庫附加到目前資料庫連接配接的機制——Attach DB。
Attach DB的使用方法依然是SQL語句,文法為:
附加:
attach [database] filename as database_name;
取消附加:
detach [database] database_name;
下面使用具體的db試用一下Attach DB,探讨一些細節。
基本用法
兩個測試資料庫test.db,test1.db
~/test_sqlite$ sqlite3 test.db
SQLite version -- ::
Enter ".help" for usage hints.
sqlite>
sqlite> .tables
test_table test_table2
sqlite>
sqlite> .schema
CREATE TABLE test_table (id integer primary key, name text, description text);
CREATE TABLE test_table2 (a TEXT, b TEXT, c TEXT, d TEXT);
CREATE INDEX test_table2_index on test_table2 (a,b,c,d);
sqlite>
sqlite> .headers on
sqlite>
sqlite> select * from test_table;
id|name|description
|name0|des0
|name1|des1
|name2|des2
|name3|des3
~/test_sqlite$ sqlite3 test1.db
SQLite version -- ::
Enter ".help" for usage hints.
sqlite>
sqlite> .tables
test_table test_table2 test_table3
sqlite>
sqlite> .schema
CREATE TABLE test_table (id integer primary key, name text, description text);
CREATE TABLE test_table2 (id integer primary key, name text, description text);
CREATE TABLE test_table3 (id integer primary key, name text, description text);
sqlite>
sqlite> .headers on
sqlite>
sqlite> select * from test_table;
id|name|description
|text0|description0
連接配接到test.db,附加test1.db:
~/test_sqlite$ sqlite3 test.db
SQLite version -- ::
Enter ".help" for usage hints.
sqlite>
sqlite> .tables
test_table test_table2
sqlite>
sqlite> attach 'test1.db' as test1;
sqlite>
sqlite> .tables
test1.test_table test1.test_table3 test_table2
test1.test_table2 test_table
從.tables指令的結果,已經能夠看到test1.db中的表了,通過select也能夠得到test1.db中的資料:
sqlite> select * from test1.test_table;
id|name|description
|text0|description0
這裡面需要注意幾個點:
(1)attach語句的第一個參數必須在單引号之内,且必須是相對于目前SQLite終端的完整路徑檔案名。
(2)attach語句的第二個參數可以用單引号,也可以不用。
(3)對于attach進來的資料庫,是一個as後面的參數(附加資料庫别名)作為ID,也就是說:如果想附加多個資料庫,需要指定不同的别名,别名不可重複;同一個資料庫可以多次附加,隻要保證使用不同的别名即可。例如:
sqlite> attach 'test1.db' as 'test2';
sqlite>
sqlite> .tables
test1.test_table test1.test_table3 test2.test_table2 test_table
test1.test_table2 test2.test_table test2.test_table3 test_table2
資料同步
如果被附加的db在其他連接配接中有資料更新,能夠同步嗎?實驗一下,連接配接一打開test.db并附加test1.db,連接配接二打開test1.db并做更新操作。
連接配接一:
~/test_sqlite$ sqlite3 test.db
SQLite version -- ::
Enter ".help" for usage hints.
sqlite>
sqlite> attach 'test1.db' as test1;
sqlite>
sqlite> .tables
test1.test_table test1.test_table3 test_table2
test1.test_table2 test_table
sqlite>
sqlite> select * from test1.test_table;
|text0|description0
sqlite>
連接配接二:
插入新的資料到test_table,并drop test_table2
~/test_sqlite$ sqlite3 test1.db
SQLite version -- ::
Enter ".help" for usage hints.
sqlite>
sqlite> .headers on
sqlite>
sqlite> .tables
test_table test_table2 test_table3
sqlite>
sqlite> select * from test_table;
id|name|description
|text0|description0
sqlite>
sqlite> .schema
CREATE TABLE test_table (id integer primary key, name text, description text);
CREATE TABLE test_table2 (id integer primary key, name text, description text);
CREATE TABLE test_table3 (id integer primary key, name text, description text);
sqlite>
sqlite> insert into test_table values (,'text1','description1');
sqlite>
sqlite> select * from test_table;
id|name|description
|text0|description0
|text1|description1
sqlite>
sqlite> drop table test_table2;
sqlite>
sqlite> .tables
test_table test_table3
sqlite>
現在,回到連接配接一,看一下資料的情況:
sqlite> .tables
test1.test_table test1.test_table3 test_table test_table2
sqlite>
sqlite> .headers on
sqlite>
sqlite> select * from test1.test_table;
id|name|description
|text0|description0
|text1|description1
看到連接配接二中的資料更新已經完全同步到連接配接一中。
反過來,在連接配接一中,對于附加進來的test1.db做更新操作:
sqlite> drop table test1.test_table3;
sqlite>
sqlite> .tables
test1.test_table test_table test_table2
sqlite>
sqlite> insert into test1.test_table values (,'text2','description2');
sqlite>
sqlite> select * from test1.test_table;
id|name|description
|text0|description0
|text1|description1
|text2|description2
回頭在連接配接二中檢視資料:
sqlite> .tables
test_table
sqlite>
sqlite> select * from test_table;
id|name|description
|text0|description0
|text1|description1
|text2|description2
看到同樣的結果,連接配接一中對于附加資料庫的資料更新也同步回了原資料庫的連接配接(連接配接二)。
是以,SQLite的Attach DB機制是一種非常寬松的機制,對于附加資料庫,可以同步和被同步。這種機制很強大和靈活,但也增加了風險性。