ATTACH DATABASE
The ATTACH DATABASE statement adds another database file to the current connection under a schema name. Once attached, tables in the external database can be queried alongside tables in the main database.Syntax
Description
ATTACH opens the database file atfilename and makes its tables accessible through the given schema-name. Tables in the attached database are referenced as schema-name.table-name.
Clauses
| Clause | Description |
|---|---|
DATABASE | Optional keyword. Has no effect on behavior. |
filename | A string expression that evaluates to the path of the database file to attach. Use ':memory:' to attach an in-memory database. |
schema-name | The name used to qualify table references from the attached database. This name must be unique among all attached databases on the connection. |
main for its primary database. The names main and temp are reserved and cannot be used as schema names for attached databases.
This feature is experimental and must be enabled before use. Attached databases are currently read-only. All write operations (INSERT, UPDATE, DELETE, CREATE TABLE, etc.) on tables in attached databases will fail. Use ATTACH for cross-database queries and data migration reads.
Examples
Attach and Query an External Database
Cross-Database Queries
Attach an In-Memory Database
See Also
- DETACH DATABASE for removing an attached database
- SELECT for querying tables across databases