ATTACH DATABASE

Updated: April 19, 2023

Attach a database file to the current connection

Synopsis:

 ATTACH [DATABASE] database-filename AS database-name 

Description:

The ATTACH DATABASE statement attaches another database file to the current database connection. If the filename contains punctuation characters, it must be placed inside quotation marks. The names main and temp refer to the main database and the database used for temporary tables. These cannot be detached. Attached database files are removed using the DETACH DATABASE statement.

You can read from and write to an attached database, and you can modify its schema.

You cannot create a new table with the same name as a table in an attached database, but you can attach a database with table names that are duplicates of those in the main database. It is also permissible to attach the same database file multiple times.

Tables in an attached database can be referred to using the syntax database-name.table-name. For tables that don't have duplicate names in the main database, they don't require a database name prefix. When a database file is attached, each of its tables without a duplicate name becomes the default table for that name. Any tables that have the same name and are contained in database files attached afterwards require the database name prefix. If the database file containing the default table for a given name is detached, then the table with the same name that was attached last becomes the new default.

Transactions involving multiple attached databases are atomic. There is a compile-time limit of 10 attached database files.