The Wayback Machine - https://web.archive.org/web/20241107182945/https://www.geeksforgeeks.org/sqlite-describe-table/
Open In App

SQLite Describe Table

Last Updated : 23 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. SQLite works on various platforms like Windows, Mac, Linux, iOS and Android. This makes popular choice for developers to develop applications for several platforms or Operating Systems.

In this article, we will see various ways to describe an SQLite Table along with the syntax, practical implementations, and so on.

Describing a Table Using SQLite

Sometimes developers want to see the structure or schema of the already created tables. SQLite provides different approaches to achieving it. Sometimes describing a table becomes very necessary when the developer didn't create the table or is unknown to the schema of it, so before inserting any values it is always recommended to check the schema of the table or the description of the table. Describing a table is also helpful when the developers or users who didn't create than table want to know whether any peripherals such as INDEX or VIEWS are associated with the Table or not.

The description of the Table also includes the details and data type of each column, such as which are NOT NULL, which is a PRIMARY KEY, which is a FOREIGN KEY, etc. This information will be helpful to the developers who are new to the table and want to manipulate it.

Ways to Describe Table in SQLite

To understand the Describe Table in SQLite we will use an already created table called Employees which consists of empID, DeptID, FirstName, LastName, Salary, and Location as Columns.If you don't know How to create a Table in SQLite then refer to this.

1. Using .Schema Command

SQLite offers a dot-command schema that can display the structure/schema of the table as well as the commands used to create that table. It also displays if there are any peripherals like INDEX or anything associated with the Table or not.

Syntax:

.schema <table_name>

In the schema dot command, we have to simply write .schema, consists, and then after a space we need to pass the table name whose structure or description we want to get.

To get the description of our Employees table using schema command.

Query:

.schema Employees

Output:

schema

As we can see in the output, the column name alongside their data type and other features like NOT NULL, PRIMARY KEY etc. with the details of Indexes associated with the Table has been given as output.

2. Using PRAGMA Command

We can also use PRAGMA command with the table_info() function to get the details and description of a table. But it is necessary to execute another command before the PRAGMA command to make the output understandable and clear.

The command is a dot-command which is used to set the mode as Columns, which will display the entire table with the column name and space separated, which is much more human readable. Query:

.mode columns

After executing this command, now we can use the pragma command to get more detailed and structured result.

The generic syntax of table_info() function is given below:

pragma table_info(<table_info>);

To get the description of the Employees table using the PRAGMA table_info() command.

Query:

pragma table_info(Employees);

Output:

pragma

Explanation: As we can see in the output, the description of all the columns, alongside their data type has been listed in a columned manner which is very much understandable and readable. Here there are also three columns named notnull, dflt_value and pk, the description of those columns are given below -

  • notnull: This column signifies whether the column has been created as a NOT NULL column or not, means that no NULL values can be present in that column. The value 1 signifies True and that column has been created as a NOT NULL column, the value 0 signifies False and that column has not been created as a NOT NULL column, that column can hold NULL values.
  • dflt_values: It is the acronym of Default Values, if any Default values were passed during the creation of the table, it would have been reflected here. If not, then this column will be left as blank.
  • pk:This is the acronym of Primary Key, just like the NOT NULL column, this column also deals with the value 0 or 1. The value 1 signifies that a particular column is being used as a Primary Key, 0 signifies it is not.

3. Getting the Structure of a Table Using the SQL Statement

We can also get the structure or the description of the table using the sql_schema table using the SELECT, FROM and WHERE statement. sql_schema is a table which holds the structure of all the tables created in that particular database.

Syntax:

select sql from sqlite_schema where name = '<table_name>';

To get the structure of the Employees table,

Query:

select sql from sqlite_schema where name = 'Employees';

Output:

sqlite_schema

4. Save the Result of a Query Into a File

SQLite provides various ways using which we can save the result of a Query into a file. There are two dot-commands which are used to achieve this. We will discuss both of them one by one.

.once Command

.once SQLite command allows the user to store the result of the immediately run query just after the execution of the once command. Means that the result of only one query ran after the execution of the once command will be stored in the desired file.

Syntax:

.once <full_path_of_the_desired_file>

If the desired file resides in the same folder as SQLite Database, then only the name and the extension of that file will suffice.

To use the .once command with the Employees Table.

Query:

.once C:\Users\user\Downloads\SQLite\once_results.txt

Now we will use the SELECT command to return all the values whose Salary is greater than 25000.

Query:

select * from employees where salary > 25000;

Contents of the once_results file:

once_result

.output Command

Another command is the output command, which is used to store the result of a query into a file. The difference of output with the .once command is that, .output will store the results of every query executed after it has been executed, unlike the once command.

Syntax:

.output <full_path_of_the_desired_file>

If the desired file resides in the same folder as SQLite Database, then only the name and the extension of that file will suffice.

To use the .output command with the Employees Table.

Query:

.output C:\Users\user\Downloads\SQLite\output_results.txt

Now, we will execute two commands -

  • First one will fetch all the details where the salary is greater than 30,000.
  • Second one will fetch all the details where the Location is 'KY'.

The output of both the commands will be stored in the provided file:

Query:

select * from employees where salary > 30000;
select * from employees where location = 'KY';

Contents of the output_results File:

OutputCommand

Explanation: As we can see that both the outputs of the commands have been stored in the provided file.

5. Execute SQL statements from a file

In this section, we will see how we can execute SQLite statements from an .SQL file. There is a dot-command read using which we can execute a SQL file, this command is used to read the contents of an SQL file and execute the commands mentioned in it.

Syntax:

 .read <full_path_of_the_file>

We are creating a file named Freshers which will hold a CREATE TABLE command which will be used to create a table called Freshers.

Query:

.read C:\Users\user\Downloads\SQLite\freshers.sql

After executing this command, if the contents of the SQL file are correct, then we will not see any kind of output on screen. As this file contained a command to create a new table, we can use the tables dot-command to see if the table has been created or not. We can also check it's schema using the .schema dot command.

Using .tables command

Using the .tables command to see if the table has been created successfully or not.

.tables

Output:

tables-command

Explanation: As we can see, the FRESHERS table has been created successfully.

Using .schema command

Using the schema command we will now see the description of the table.

Query:

.schema freshers

Output:

schema_freshers

Explanation: As we can see in the output that the description of the Freshers table has been displayed.

Conclusion

We saw in this article how we can describe a SQLite table using various methods. Learning about the structure of the table becomes important when the developer wants to learn about the structure or schema of a table created by someone else, so that it becomes easy to understand the data types of the columns, which column will accept NULL values, is there any peripherals like INDEX associated with the table or not etc.

SQLite provides various methods and all of the methods are unique in their own place, they all are used to get the same results but the structure and the informations mentioned or returned are somewhat different. It is always recommened to get the description or learn about the structure of the table before trying to manipulate it, to avoid any kind of circumstances which might affect in the database or the table severely.


Similar Reads

How to Update a Table Data From Another Table in SQLite
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queries and operations on the database. In this article
3 min read
SQL Server Describe Table
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting information about the structure and metadata of the table.
4 min read
MySQL Describe Table
MySQL is an open-source relational database management system (RDBMS). It is one of the most popular databases globally, known for its reliability, and scalability. MySQL is designed to run on various operating systems, including Windows, Linux, macOS, and more. It is known for its high performance, quick retrieval, and efficient storage. It provid
6 min read
SQLite Create Table
SQLite is a database engine. It does not require any server to process queries. It is a kind of software library that develops embedded software for television, smartphones, and so on. It can be used as a temporary dataset to get some data within the application due to its efficient nature. It is preferable for small datasets. SQLite CREATE TABLESQ
2 min read
SQLite Alter Table
SQLite is a serverless architecture that we use to develop embedded software for devices like televisions, cameras, and so on. It is written in C programming Language. It allows the programs to run without any configuration. In this article, we will learn everything about the ALTER TABLE command present in SQLite. In SQLite, the ALTER TABLE stateme
4 min read
Import a CSV File Into an SQLite Table
SQLite is a lightweight, embedded relational database management system (RDBMS)which famous for its simplicity and fewer setup requirements. It is a self-contained, serverless, and zero-configuration database engine, SQLite is widely used in various applications, including mobile devices, desktop software, and embedded systems. SQLite supports stan
3 min read
How to Update Table Rows in SQLite Using Subquery
SQLite is a lightweight, serverless RDBMS that is used to store and manipulate the data in the database in relational or tabular form. It can run on various operating systems like Windows, macOS, Linux and mobile operating systems like Android and iOS. SQLite is designed for single-user access. Multiple processes can read from an SQLite database si
4 min read
How to Find Records From One Table Which Don't Exist in Another SQLite?
In database management, one of the most common tasks is to compare records either to identify differences or missing records in certain tables. This phase is crucial for data validation, reconciliation, and complete data integrity. On SQLite, a lightweight relational database management system, this is probably the most straightforward task to do t
4 min read
How to Export Database and Table Schemas in SQLite?
Exporting database schemas in SQLite is an important task for database management, enabling functions like data backup, recovery, migration, and auditing. In this article, We will go through the process of exporting database and table schemas in SQLite by understanding various examples to manage SQLite databases effectively. How to Export Database
4 min read
How to Select the Nth Row in a SQLite Database Table?
In SQLite, selecting a specific row from a table can be a common requirement, especially when dealing with large datasets. In this article, we will explore different methods to select the nth row from a SQLite database table. Whether we're a beginner or an experienced developer understanding these methods can help us efficiently retrieve specific d
4 min read
How to Select Rows with no Matching Entry in Another Table in SQLite?
In database management, selecting rows from one table that does not have matching entries in another table means returning the rows that are present in one table but do not have the same entry in any other table. This scenario often arises in various data validation and analysis processes. In this article, We will learn about How to select rows wit
4 min read
How to Select Rows from a Table that are Not in Another Table?
In MySQL, the ability to select rows from one table that do not exist in another is crucial for comparing and managing data across multiple tables. This article explores the methods to perform such a selection, providing insights into the main concepts, syntax, and practical examples. Understanding how to identify and retrieve rows that are absent
3 min read
Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another table, using an ID match. ID match ensures that the
3 min read
SQLite LIKE Operator
SQLite is a serverless architecture that we use to develop embedded software for devices like televisions, cameras, and so on. It is written in C programming Language. It allows the programs to run without any configuration. In this article, we will learn everything about the LIKE operator. After reading this article you will get decent knowledge a
4 min read
SQLite Replace Statement
SQLite is a database engine. It is a serverless architecture as it does not require any server to process queries. Since it is serverless, it is lightweight and preferable for small datasets. It is used to develop embedded software. It is cross-platform and available for various Operating systems such as Linux, macOS, Windows, Android, and so on. R
3 min read
SQLite Transaction
SQLite is a database engine. It is a software that allows users to interact with relational databases. Basically, it is a serverless database which means it does not require any server to process queries. With the help of SQLite, we can develop embedded software without any configurations. SQLite is preferable for small datasets. SQLite is a portab
5 min read
SQLite Primary Key
SQLite is an open-source database system just like SQL database system. It is a lightweight and serverless architecture which means it does not require any server and administrator to run operations and queries. It is widely used by the developer to store the data within the applications. It is preferable for small datasets without much effort. It
4 min read
SQLite Foreign Key
SQLite is a serverless architecture, which does not require any server or administrator to run or process queries. This database system is used to develop embedded software due to its lightweight, and low size. It is used in Desktop applications, mobile applications televisions, and so on. Foreign Key in SQLiteA Foreign Key is a column or set of co
3 min read
SQLite Update Statement
SQLite is a database engine. It is a software that allows users to interact with relational databases, Basically, it is a serverless database which means it does not require any server to process queries. With the help of SQLite, we can develop embedded software without any configurations. SQLite is preferable for small datasets. Update StatementSo
3 min read
SQLite WHERE Clause
SQLite is the serverless database engine that is used most widely. It is written in c programming language and it belongs to the embedded database family. In this article, you will be learning about the where clause and functionality of the where clause in SQLite. Where ClauseSQLite WHERE Clause is used to filter the rows based on the given query.
3 min read
SQLite SELECT Query
SQLite is a serverless, popular, easy-to-use, relational database system that is written in c programming language. SQLite is a database engine that is built into all the popular devices that we use every day of our lives including TV, Mobile, and Computer. As we know that we create the tables in the database and insert the data into them to store
6 min read
SQLite ORDER BY Clause
SQLite is the most popular database engine which is written in C programming language. It is a serverless, easy-to-use relational database system and it is open source and self-contained. In this article, you will gain knowledge on the SQLite ORDER BY clause. By the end of this article, you will get to know how to use the ORDER BY clause, where to
8 min read
SQLite UNIQUE Constraint
SQLite is a lightweight relational database management system (RDBMS). It requires minimal configuration and it is self-contained. It is an embedded database written in C language. It operates a server-less, file-based database engine making it a good fit for mobile applications and simple desktop applications. It supports standard SQL syntax. In t
4 min read
SQLite COUNT
SQLite is a serverless database engine, it is written in C programming language and is easy to use and store the data. It is one of the most used database engines. It is widely used for the development of Embedded applications. In this article we will learn about the SQLite count, how it works and what are all the other functions that are used with
6 min read
SQLite Autoincrement
SQLite is a serverless database engine written in c programming language. It is one of the most used database engines in our everyday life like Mobile Phones, TV, and so on, etc. In this article, we will be learning about autoincrement in SQLite, its functionality, and how it works along with the examples and we will also be covering without autoin
5 min read
SQLite Limit clause
SQLite is the most popular and most used database engine which is written in c programming language. It is serverless and self-contained and used for developing embedded software devices like TVs, Mobile Phones, etc. In this article, we will be learning about the SQLite Limit Clause using examples simply and understandably. How does the SQLite limi
5 min read
SQLite CHECK Constraint
SQLite is a lightweight and embedded Relational Database Management System (commonly known as RDBMS). It is written in C Language. It supports standard SQL syntax. It is a server-less application which means it requires less configuration than any other client-server database (any database that accepts requests from a remote user is known as a clie
5 min read
SQLite IS NULL
SQLite is a server-less database engine and it is written in c programming language. It is developed by D. Richard Hipp in the year 2000. The main moto for developing the SQLite is to escape from using the complex database engines like MYSQL.etc. It has become one of the most popular database engines as we use it in Television, Mobile Phones, web b
6 min read
SQLite MIN
SQLite is a serverless database engine with almost zero or no configuration headache and provides a transaction DB engine. SQLite provides an in-memory, embedded, lightweight ACID-compliant transactional database that follows nearly all SQL standards. Unlike other databases, SQLite reads and writes the data into typical disk files within the system
4 min read
SQLite HAVING Clause
SQLite is a server-less database engine and it is written in c programming language. The main moto for developing SQLite is to escape from using complex database engines like MySQL etc. It has become one of the most popular database engines as we use it in Television, Mobile Phones, web browsers, and many more. It is written simply so that it can b
5 min read
three90RightbarBannerImg