Sqlite Detach Database
# SQLite Detach Database
SQLite's **DETACH DATABASE** statement is used to detach and disassociate a named database from a database connection that was previously attached using the ATTACH statement. If the same database file has been attached with multiple aliases, the DETACH command will only disconnect the given name, while the others remain valid. You cannot detach the **main** or **temp** database.
> If the database is in-memory or a temporary database, the database will be destroyed and its contents will be lost.
## Syntax
The basic syntax of SQLite's DETACH DATABASE 'Alias-Name' statement is as follows:
DETACH DATABASE 'Alias-Name';
Here, 'Alias-Name' is the same alias you used when attaching the database with the ATTACH statement.
## Example
Assuming you have created a database in previous chapters and attached 'test' and 'currentDB' to it, using the .database command, we can see:
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
3 currentDB /home/sqlite/testDB.db
Now, let's try to detach 'currentDB' from testDB.db, as shown below:
sqlite> DETACH DATABASE 'currentDB';
Now, if you check the currently attached databases, you will find that testDB.db is still connected to 'test' and 'main'.
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
[](#)(#)
(#)[](#)
YouTip