Comprehensive Guide to Viewing Table Structure in SQLite

Nov 20, 2025 · Programming · 17 views · 7.8

Keywords: SQLite | table structure | PRAGMA | schema command | sqlite_master

Abstract: This article provides a detailed exploration of three primary methods for viewing table structure in SQLite databases: using the PRAGMA table_info command to obtain field information, employing the .schema command to display CREATE statements, and directly querying the sqlite_master system table. Through concrete code examples and output comparisons, the article offers in-depth analysis of each method's applicable scenarios and trade-offs, assisting developers in selecting the most appropriate approach for table structure inspection based on practical requirements.

Overview of Table Structure Viewing Methods in SQLite

In database management and development, inspecting table structure is a fundamental and crucial operation. Similar to the desc command in Oracle, SQLite offers multiple approaches to retrieve table metadata. This article delves into three primary methods: PRAGMA commands, special commands in the SQLite command-line tool, and direct queries to system tables.

Using the PRAGMA table_info Command

PRAGMA is a SQLite-specific command used to query and modify the internal state of the database. The PRAGMA table_info(table_name) command returns detailed information about the columns of a specified table, including column names, data types, nullability, default values, and primary key status.

Example code:

PRAGMA table_info('albums');

The output typically includes the following columns:

This method is suitable for scenarios requiring detailed column information, particularly in programming environments through direct SQL queries.

Using the SQLite Command-Line Tool

SQLite provides a powerful command-line tool, sqlite3, which includes several special dot commands for convenient database management.

.tables Command

The .tables command lists all table names in the current database:

sqlite> .tables
addresses    ews_folders   subjects
alarms       feeds         threads
associations mailboxes     todo_notes
attachments  messages      todos
calendars    properties    todos_deleted_log
events       recipients    todos_server_snapshot

.schema Command

The .schema [table_name] command displays the CREATE TABLE statement for the specified table, providing an intuitive way to understand table structure:

sqlite> .schema alarms
CREATE TABLE alarms (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, alarm_id,
                     todo INTEGER, flags INTEGER, offset_days INTEGER,
                     reminder_date INTEGER, time INTEGER, argument,
                     unrecognized_data BLOB);
CREATE INDEX alarm_id_index ON alarms(alarm_id);
CREATE INDEX alarm_todo_index ON alarms(todo);

It is important to note that when using the .schema command, do not add a semicolon after the table name, as SQLite will treat the entire string table_name; as the table name.

Querying the sqlite_master System Table

SQLite stores metadata for all database objects in a system table called sqlite_master. By directly querying this table, you can obtain the complete definition of a table:

SELECT sql 
FROM sqlite_master 
WHERE name = 'albums';

The execution result returns the complete SQL statement used to create the albums table:

CREATE TABLE "albums"
(
[AlbumId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Title] NVARCHAR(160) NOT NULL,
[ArtistId] INTEGER NOT NULL,
FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])
ON DELETE NO ACTION ON UPDATE NO ACTION
)

This method offers maximum flexibility, allowing complex SQL queries to filter and combine required metadata information.

Method Comparison and Selection Recommendations

Each of the three methods has distinct advantages and is suitable for different usage scenarios:

In practical development, it is recommended to choose the appropriate method based on specific needs. For daily database management, the .schema command is typically the most convenient choice, while in application programming, PRAGMA table_info or direct queries to sqlite_master may be more suitable.

Output Format Optimization

When using PRAGMA commands, output readability can be enhanced by setting the output format:

.header on
.mode column
PRAGMA table_info('albums');

With these settings, the output will include column headers and display in aligned column format, making it easier to read and analyze.

Conclusion

SQLite offers multiple flexible approaches to view table structure, each with specific application scenarios. Mastering these methods is essential for database development and management. Through the explanations in this article, readers should be able to select the most appropriate table structure viewing method based on their practical requirements, thereby improving efficiency and quality in database-related work.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.