Easy Tutorial
❮ Sqlite Autoincrement Sqlite Unions Clause ❯

SQLite Detach Database

The DETACH DATABASE statement in SQLite is used to separate a named database from a database connection, which 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 databases.

If the database is in memory or a temporary database, it will be destroyed, and its contents will be lost.

Syntax

The basic syntax of the SQLite 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

Suppose you have created a database in a previous section and attached it with 'test' and 'currentDB' 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 follows:

sqlite> DETACH DATABASE 'currentDB';

Now, if you check the currently attached databases, you will find that testDB.db is still connected with 'test' and 'main'.

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db
2    test             /home/sqlite/testDB.db
❮ Sqlite Autoincrement Sqlite Unions Clause ❯