PostgreSQL INTEGER Data Type With Examples

PostgreSQL provides the INTEGER or INT data type to store the 32-bit of the integer values. It is the common data type that is utilized for storing numeric values. Its range starts from -2,147,483,648 to +2,147,483,647 numbers.

The INTEGER data type is classified into multiple types, i.e., INTEGER, BIGINT, and SMALLINT. These data types have different storage sizes and ranges for storing numeric values. Users can not store the values which are exceeding the allocated range of a particular data type otherwise will encounter an error. Today, we will guide you about the INTEGER data type along with different examples in PostgreSQL.

The content that illustrates the usage of the INTEGER data types is given as.

  • Example 1: Create a Column With INTEGER Data Type in PostgreSQL
  • Example 2: Insert INTEGER Data to a Table in PostgreSQL

Let's start with the first example.

Example 1: Create a Column With INTEGER Data Type in PostgreSQL

First of all, let’s create a table in the PostgreSQL database using the “CREATE TABLE” statement:

CREATE TABLE school_info( 
std_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
fee INT NOT NULL);

The table consists of three columns: std_id, name, and fee. The std_id and fee columns are initialized with the INTEGER data type to store the numerical values:

img

The “school_info” table with three columns std_id, name, and fee has been created successfully. After that, the user can verify the structure of the table through the “SELECT” statement:

SELECT * FROM school_info;
img

The output shows that “std_id”, “name” and “fee” columns have been created with integer, text and integer data types, respectively.

Example 2: Insert INTEGER Data to a Table in PostgreSQL

Another example is considered with the help of the “INSERT INTO” statement to insert values in the “std_id”, “name” and “fee” columns. For instance, the statement is provided below:

INSERT INTO school_info(std_id, name, fee)
VALUES(1, 'Peter', 4500),
(2, 'Harry', 2000),  
(3, 'Willey',5000), 
(4, 'Henry', 1700),
(5, 'John', 5000);
img

The integer values have been successfully inserted in the “std_id” and “fee” columns of the table. To display all values of the table, use the “SELECT *” statement followed by the table name as shown in the below statement:

SELECT * FROM school_info;
img

This way, users can insert the integer data to any specific table.

Conclusion

In PostgreSQL, the INTEGER data type facilitates the user to store the numerical data between the range of -2,147,483,648 to +2,147,483,647 numbers. This data type is most widely utilized for storing numbers in tables. This article has explained the usage of INTEGER data type along with practical implementation in the PostgreSQL database.