Open In App

How to Check Column Type in SQLite?

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

SQLite is a lightweight and relational database management system. SQLite is used to develop embedded software for devices like televisions, cell phones, cameras, etc. SQLite is a serverless database system which means it does not require any server to process queries.

In this article, we will how to check column type in SQLite with multiple methods and examples and so on.

Introduction to Different Ways to Check Column Types in SQLite

To understand How to Check Column Type in SQLite we will see some approaches with examples and explanations.

Approach 1: Using PRAGMA Statement

The PRAGMA statement in SQLite offers a convenient way to obtain metadata about the database, including column types. Specifically, the table_info pragma allows us to retrieve information about columns in a table.

Example 1: Fetch Information about Columns of Employees Table

Let’s retrieve information about the columns of the “employees” table using the PRAGMA statement.

Query:

PRAGMA table_info(employees);

Output:

Examples1usingPrgma

Output

Explanation:

  • cid: It is a Column ID.
  • name: It is a Column name.
  • type: It is a Data type of the column.
  • notnull: It is a 1 if the column cannot contain NULL values, 0 otherwise.
  • dflt_value: It is a Default value for the column.
  • pk: It is a 1 if the column is part of the primary key, 0 otherwise.

Approach 2: Using Querying SQLite Master Table

Another method involves querying the SQLite master table, which stores metadata about database objects, including tables.

Example 1: Fetch the Definition of employees Table

We will retrieve the definition of a table named ‘employees‘ from the SQLite database. The query check the ‘sqlite_master‘ table and return the definition if the ‘employees‘ table exists else it indicate that the table does not exist.

Query:

SELECT sql FROM sqlite_master WHERE name = 'employees';

Output:

SQliteMaster

Output

Explanation: The CREATE TABLE statement show the columns with their data types.

In the output table, we can see the SQL statement used to create the “employees” table. This statement add the column names and their corresponding data types. This method is useful for obtaining the original table creation and providing a comprehensive view of the database schema.

Approach 3: Using the typeof() Function

The typeof() function in SQLite allows us to determine the type of a value or expression. By applying this function to a column, we can retrieve its data type.

Example 1: Fetch the Data Type of employee_name column From employees Table

Now we will retrieve the data type of the ‘employee_name‘ column from the ‘employees‘ table in SQLite. The typeof function is employed to determine the data type of the specified column.

Query:

SELECT typeof (employee_name) FROM employees;

Output:

typeof

Output

Explanation: The result indicates that the data type of the “employee_name” column is TEXT.

Approach 4: PRAGMA table_info() and typeof() Combination

For a more detailed examination, we can combine the PRAGMA statement and the typeof() function in a single query.

Example 1: Fetch Information About Columns in employees Table

We will retrieves information about the columns in the ‘employees‘ table which includes the column names and their data types. The pragma_table_info function is used to obtain details about table structure.

Query:

SELECT
name AS column_name,
type AS column_type
FROM
pragma_table_info('employees');

Output:

tableInfo

Output

Explanation: This query returns a clear table with “column_name” and “column_type” for each column in the “employees” table.

Conclusion

Overall, after reading the whole article now we have a good Understanding of how to check column types in SQLite is fundamental for efficient database management. The methods we use which are PRAGMA statements, querying the SQLite master table, and employing functions like typeof(),offers flexibility and depth in understanding column types.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads