How can I list the tables in a SQLite database file that was opened with ATTACH?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with SQLite databases, you might find yourself needing to manage multiple databases at once. This scenario often arises during data migration, analytics on disparate data sources, or when organizing a complex dataset split across multiple files. SQLite facilitates this process using the ATTACH DATABASE command, which allows you to connect multiple databases in a single session. Once databases are attached, you may need to explore the tables across these databases. In this article, we’ll cover how to list tables in SQLite database files after they are opened with the ATTACH command.
Understanding ATTACH DATABASE in SQLite
The ATTACH DATABASE command in SQLite is used to attach another database file to the current connection. This lets you perform operations that involve multiple databases. For example:
Here, 'path/to/your/secondary.db' is the file path of the second database, and secondary is the alias you assign to it within your current session. After executing the command, you can refer to this database with the alias in your SQL queries.
Listing Tables in Attached Databases
After attaching one or more databases, you might need to list all available tables in either the main database or the attached databases. SQLite provides a convenient and standardized way to query metadata about a database's structure, including its tables, using the sqlite_master table or the sqlite_temp_master table for temporary databases.
Here’s how you can query this metadata:
- List All Tables in All Databases: To list all tables across the main and attached databases, you can use the following SQL query:
In this SQL script, secondary refers to the alias used during the ATTACH command. Replace secondary with the actual alias used when attaching the database. You may repeat the UNION ALL part for each attached database.
- List Tables from a Specific Database: If you're interested in listing tables from a specific attached database, omit the
UNION ALLclause and query only from the relevantsqlite_mastertable:
Replace secondary with the alias of your specific database.
Example Scenario
Suppose you have two databases: main.db, and you've attached secondary.db as "secondary", you could use the following command snippet to list all tables from both databases:
Summary of Key Points
Here's a summary table of key commands and considerations when working with attached SQLite databases:
| Task | SQL Command |
| Attach a database | ATTACH DATABASE 'filepath' AS alias; |
| List all tables (all DBs) | SELECT name FROM sqlite_master UNION ALL SELECT name FROM alias.sqlite_master WHERE type='table'; |
| List tables from main DB | SELECT name FROM sqlite_master WHERE type='table'; |
| List tables from attached DB | SELECT name FROM alias.sqlite_master WHERE type='table'; |
Conclusion
Using the ATTACH DATABASE command with SQLite allows for powerful multi-database operations. Listing tables across these databases is straightforward once you understand the use of sqlite_master tables specific to each database context. This capability is particularly useful in complex applications or during transitional tasks such as database merging or migration. Always ensure your paths and aliases are correctly set to avoid any querying issues.

