SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite - DETACH DATABASE



The SQLite DETACH DATABASE statement is used to detach and dissociate a named database from the current database connection which was previously attached using ATTACH statement. If the same database file has been attached with multiple aliases, then DETACH command will disconnect only the given name and rest of the attachment will still continue.

Syntax

The syntax of using SQLite DETACH DATABASE statement is given below:

DETACH [DATABASE] AS database_name;

Parameters

DATABASE Optional. The command can be run as either DETACH DATABASE or DETACH.
database_name Required. Specify the logical name for the database file that need to be detached from the current database connection.

Example: Detach a named database

Lets first use the .database dot command to see the list of databases available in the current connection.

sqlite> .database
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db
2    example          /home/sqlite/example.db

To detach the example database from the current connection, the following command can be used:

sqlite> DETACH DATABASE 'example';

After detaching the database, the .database dot command can be used again to see the list of databases available in the current connection.

sqlite> .database
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db

The database names main and temp are reserved for the primary database and database to hold temporary tables and other temporary data objects. Both of these database names exist for every database connection and can not be used for attachment or detachment.