Skip to main content

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

ATTACH [DATABASE] filename AS schema-name;

Description

ATTACH opens the database file at filename and makes its tables accessible through the given schema-name. Tables in the attached database are referenced as schema-name.table-name.

Clauses

ClauseDescription
DATABASEOptional keyword. Has no effect on behavior.
filenameA string expression that evaluates to the path of the database file to attach. Use ':memory:' to attach an in-memory database.
schema-nameThe name used to qualify table references from the attached database. This name must be unique among all attached databases on the connection.
Every connection has a built-in schema named 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

-- Attach another database file
ATTACH DATABASE 'archive.db' AS archive;

-- Query a table in the attached database
SELECT * FROM archive.events WHERE event_date > '2025-01-01';

Cross-Database Queries

ATTACH 'customers.db' AS customers_db;

-- Join tables across the main and attached databases
SELECT o.id, o.total, c.name
FROM main.orders o
JOIN customers_db.customers c ON o.customer_id = c.id;

Attach an In-Memory Database

ATTACH ':memory:' AS scratch;

See Also