ATTACH DATABASE and DETACH DATABASE
Hello SQLite guys.
what ATTACH DATABASE and DETACH DATABASE do? I have not seen any SQL database systems have these two SQL statements.
Microsoft SQL also has this, see: https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database?view=sql-server-ver15
For SQLite see: https://www.sqlite.org/lang_attach.html
(3) By Gunter Hick (gunter_hick) on 2020-10-08 16:31:57 in reply to 1 [link] [source]
ATTACH allows you to access (open) another SQLite database file in your current session. DETACH tells SQLite that you are finished with the database file so that SQLite can close it.
But I see some SQLite tutorials attach the same database file multiple times, what does it mean then? For example, ATTACH DATABASE my.db AS name1 ATTACH DATABASE my.db AS name2 ... What it mean for ATTACH DATABASE and DETACH DATABASE in these cases?
(5) By David Raymond (dvdraymond) on 2020-10-08 18:16:37 in reply to 4 [link] [source]
But I see some SQLite tutorials attach the same database file multiple times, what does it mean then?
I can't think of a single reason why someone would want to do that. Do you have an example tutorial page where they're showing that?
In practical purposes it basically just means you have more than 1 name for the same thing.
The page is here: https://www.tutorialspoint.com/sqlite/sqlite_detach_database.htm in the Example section. sqlite>.databases seq name file --- --------------- ---------------------- 0 main /home/sqlite/testDB.db 2 test /home/sqlite/testDB.db 3 currentDB /home/sqlite/testDB.db Are "main" "test" and "currentDB" just C pointers to the same database file(testDB.db)?
This is an example of the DETACH command. It does not speak to the utility of ATTACHing the same database file multiple times.
It is sort of like saying that you could (if you really wanted to do so) cut off your sex organs with pinking shears. It does not mean that there is necessarily any utility in doing so.