SQLite
Database Management
ATTACH command
Listing Tables
Database Files

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:

sql
ATTACH DATABASE 'path/to/your/secondary.db' AS secondary;

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:

  1. List All Tables in All Databases: To list all tables across the main and attached databases, you can use the following SQL query:
sql
1   SELECT name, type, tbl_name, rootpage, sql
2   FROM sqlite_master
3   WHERE type='table'
4   UNION ALL
5   SELECT name, type, tbl_name, rootpage, sql
6   FROM secondary.sqlite_master
7   WHERE type='table';

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.

  1. List Tables from a Specific Database: If you're interested in listing tables from a specific attached database, omit the UNION ALL clause and query only from the relevant sqlite_master table:
sql
   SELECT name FROM secondary.sqlite_master WHERE type='table';

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:

sql
1-- List tables from main database
2SELECT name FROM sqlite_master WHERE type='table';
3
4-- List tables from attached 'secondary' database
5SELECT name FROM secondary.sqlite_master WHERE type='table';

Summary of Key Points

Here's a summary table of key commands and considerations when working with attached SQLite databases:

TaskSQL Command
Attach a databaseATTACH 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 DBSELECT name FROM sqlite_master WHERE type='table';
List tables from attached DBSELECT 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.


Course illustration
Course illustration

All Rights Reserved.