TutorialsArena

PostgreSQL: Understanding the INTEGER Data Type

Learn about PostgreSQL's INTEGER data type, its characteristics, usage, and considerations for choosing the right integer type for your needs.



PostgreSQL's `INTEGER` Data Type

In PostgreSQL, the `INTEGER` data type (also known as `INT`) is used to store integer values. This guide explains its characteristics, usage, and considerations when choosing an integer type in PostgreSQL.

Understanding PostgreSQL's `INTEGER` Data Type

PostgreSQL offers three main integer types: `INTEGER`, `SMALLINT`, and `BIGINT`. `INTEGER` is often the best balance between storage size, performance, and the range of values it can hold. It is a 4-byte signed integer that can accommodate numbers from -2,147,483,648 to 2,147,483,647.

Integer Data Types in PostgreSQL

Data Type Storage Size Minimum Value Maximum Value
INTEGER 4 bytes -2,147,483,648 2,147,483,647
SMALLINT 2 bytes -32,768 32,767
BIGINT 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Note: PostgreSQL does not support unsigned integer types. Attempting to store a value outside the allowed range results in an error.

Example: Storing Active Users on Social Media

(Note: Screenshots from the original text are not included here. Please refer to the original document for visual confirmation. The descriptions below aim to convey the information in those screenshots.)

  1. Create Table: Create a `Social_site` table to store the number of active users on various social media platforms.
  2. Insert Data: Insert data into the table. Note that attempting to insert a value larger than `INTEGER`'s maximum will result in an error.
  3. Retrieve Data: Use `SELECT` to retrieve all data from the table.

Example: Storing Country Populations

(Note: Screenshots from the original text are not included here. Please refer to the original document for visual confirmation. The descriptions below aim to convey the information present in those screenshots.)

  1. Create Table: Create a `countries_citizen` table to store the populations of different countries.
  2. Insert Data: Insert data into the table.
  3. Retrieve Data: Use `SELECT` to retrieve all data from the table.