当前位置:主页 > 查看内容

SQLlite数据库中的附加和分离

发布时间:2021-08-03 00:00| 位朋友查看

简介:在SQLlite数据库中往往一个数据文件就是一个schema,但是在平时的业务或者是一些条件中可能是不同的内容存放在不同的schema中,即不同的数据文件,有的场景下需要数据关联时就可以使用SQLlite的数据附加来建立一个临时的链接。如下,在使用my_test的schema时……

在SQLlite数据库中往往一个数据文件就是一个schema,但是在平时的业务或者是一些条件中可能是不同的内容存放在不同的schema中,即不同的数据文件,有的场景下需要数据关联时就可以使用SQLlite的数据附加来建立一个临时的链接。如下,在使用my_test的schema时需要关联查询一个为my_test2的schema就可以使用附加:

  1. [root@localhost data]# sqlite3 my_test.db #在SQLlite数据库中缺省database名为main 
  2. SQLite version 3.6.20 
  3. Enter ".help" for instructions 
  4. Enter SQL statements terminated with a ";" 
  5. sqlite> .database 
  6. seq  name             file                                                      
  7. ---  ---------------  ---------------------------------------------------------- 
  8. 0    main             /data/my_test.db 
  9. sqlite> ATTACH DATABASE '/data/my_test2.db' As 'my_test2'; #在当前schema下附加上/data/my_test2.db中的数据,并且起一个别名为my_test2,当然也可以起其他的名字 
  10. sqlite> .databases 
  11. seq  name             file                                                      
  12. ---  ---------------  ---------------------------------------------------------- 
  13. 0    main             /data/my_test.db                                          
  14. 2    my_test2         /data/my_test2.db 
  15. sqlite> CREATE TABLE my_test2.test_attach ( 
  16.    ...>   a int(10), 
  17.    ...>   b int(10) 
  18.    ...> ); 
  19. sqlite> SELECT * FROM my_test2.sqlite_master WHERE type = 'table' AND tbl_name = 'test_attach';  #直接在当前schema下使用/data/my_test2.db中的数据,并且查看 
  20. table|test_attach|test_attach|4|CREATE TABLE test_attach ( 
  21.   a int(10), 
  22.   b int(10) 
  23. sqlite> .exit 
  24. [root@localhost data]# sqlite3 /data/my_test2.db #切换成my_test2.db的schema查看验证下 
  25. SQLite version 3.6.20 
  26. Enter ".help" for instructions 
  27. Enter SQL statements terminated with a ";" 
  28. sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'test_attach'
  29. CREATE TABLE test_attach ( 
  30.   a int(10), 
  31.   b int(10) 

如此就是在SQLlite数据库中的附加数据库,它其实是一个链接,用于在不同的数据schma数据文件下使用其他的schma数据文件,在这里需要注意的是目前在SQLlite数据库中附加是临时的,在当前session中创建一个链接,如果在退出这个session后附加就自动分离:

  1. [root@localhost data]# sqlite3 /data/my_test.db  
  2. SQLite version 3.6.20 
  3. Enter ".help" for instructions 
  4. Enter SQL statements terminated with a ";" 
  5. sqlite> .database 
  6. seq  name             file                                                      
  7. ---  ---------------  ---------------------------------------------------------- 
  8. 0    main             /data/my_test.db 
  9. 当然有如果有附件数据库那一定有分离,分离就比较简单: 
  10.  
  11. sqlite> .databases 
  12. seq  name             file                                                      
  13. ---  ---------------  ---------------------------------------------------------- 
  14. 0    main             /data/my_test.db                                          
  15. 2    my_test2         /data/my_test2.db 
  16. sqlite> DETACH DATABASE "my_test2"
  17. sqlite> .databases                  
  18. seq  name             file                                                      
  19. ---  ---------------  ---------------------------------------------------------- 
  20. 0    main             /data/my_test.db 

这样就成功的主动分离附加在当前schma下的其他数据文件,在这里要特别注意的是如果分离的数据库是在内存或临时空间内,分离后会销毁其分离的数据。


本文转载自网络,原文链接:http://www.linuxidc.com/Linux/2018-01/150008.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐