PostgreSQL INTEGERIn this section, we are going to understand the working of the PostgreSQL Integer data type. And we also see examples of the Integer data type. What is PostgreSQL Integer Data Type?In PostgreSQL, the INTEGER data type is also known as INT. The PostgreSQL Integer data type has been classified into three types which are as follows:
In this section, we are going to understand the INTEGER Data type with examples. And we will cover the SMALLINT and BIGINT data type in the PostgreSQL tutorial. The PostgreSQL Integer data types involves 4 bytes of storage size and store integers in the signed and unsigned ranges. And the Signed range starts from -2147483648 to 2147483647. And the unsigned range began with 0 to 4294967295. The size parameter is used to define the maximum display width that is 255. Note: PostgreSQL does not allow us to use the unsigned integer types.The INTEGER data type is the most commonly used for range, performance, and size storage as compared to other integer types (smallint, bigint). The Integer data type is beneficial when we are storing data such as the number of active users on a social media application like (Facebook, Instagram, etc.), and the population of a city, or the country, and so on. We have the following table, which contains all the Integer data types specification that is supported by PostgreSQL:
Note: PostgreSQL will raise an error if we try to store a value outside of the given range.Syntax of PostgreSQL Integer data typeThe syntax of the PostgreSQL Integer data type is as follows: Examples of PostgreSQL Integer data typeLet us see different examples to understand how the PostgreSQL Integer data type works. We are creating one new table as Social_site with the CREATE command's help and inserting some values using the INSERT command. We are going to create Social_site into a javatpoint database by using the CREATE command to store the number of active users on several social media applications. Output The Social_site table has been successfully created after executing the above command, as shown in the below screenshot: Once the Social_site table has been generated, we can insert some values into it using the INSERT command. Output After executing the above command, we will get the below message: if the value is beyond the range of Integer data type (2147483647), then the PostgreSQL will show an error as integer out of range. So, now we will insert the value for the Facebook active user under the range of Integer data type, as we can see in the following command: Output After executing the above command, we will get the following result, which displays that the value has been inserted successfully into the Social_site table. As we can see in the above screenshot, the multiple values have been inserted successfully into the Social_site table. After creating and inserting the Social_site table's values, we will use the SELECT command to return all rows of the Social_site table: Output After successfully implementing the above command, we will get the following result: Example2Let us see one more example to learn the Integer data type in detail. So, we are going to create another new table as countries_citizen table by using the CREATE command into a similar database that is javatpoint to store the citizen of several countries: Note: We can also use INT instead of INTEGER as both the alternative word of each other.Output The countries_citizen table has been successfully created after executing the above commands, as shown in the below screenshot: Once the countries_citizen table has been generated, we will insert some values into it using the INSERT command as shown in the below command: Output We will get the following message on implementing the above command: the value has been inserted successfully into the countries_citizen table. After creating and inserting the countries_citizen table's values, we will use the SELECT command to return all rows of the countries_citizen table: Output After implementing the above command, we will get the following output: OverviewIn the PostgreSQL Integer data type section, we have learned that it can be the best balance between storage range, performance, and size. Next TopicPostgreSQL SMALLLINT |