PostgreSQL – INTEGER Data Type

The integer data type is used to store whole numbers (integers) within the range of -2,147,483,648 to 2,147,483,647 in PostgreSQL database. It’s a 32-bit signed integer type. Here are some key points about the integer data type in PostgreSQL:

  1. Size and Range: An integer takes up 4 bytes of storage and can represent values from -2,147,483,648 to 2,147,483,647.
  2. Alias: The integer type is sometimes referred to as int for short. You can use either integer or int when defining columns of this type.
  3. Arithmetic Operations: You can perform arithmetic operations (addition, subtraction, multiplication, division, etc.) on integer values just like any other numeric type.
  4. Numeric Functions: You can use various mathematical functions and operators with integer values, such as +, -, *, /, % (modulo), ^ (exponentiation), and more.
  5. Overflow Behavior: If an arithmetic operation results in a value outside the range of the integer data type, an overflow occurs. PostgreSQL will wrap around the values according to their representation as signed integers.

Here’s an example of using the integer data type:

CREATE TABLE employees ( employee_id serial PRIMARY KEY, first_name varchar(50), last_name varchar(50), age integer );
INSERT INTO employees (first_name, last_name, age) VALUES ('John', 'Doe', 28), ('Jane', 'Smith', 35); 
SELECT * FROM employees WHERE age > 30;

In this example, an age column of type integer is used to store employees’ ages. The integer data type allows you to perform queries and calculations involving these ages effectively.

Similar Posts