Updated May 19, 2023
Introduction to PostgreSQL Integer
The integer data types may require different storage sizes and might store negative values. PostgreSQL Integer does not allow us to store unsigned integer data types. In order to store the whole number, we have to use the integer data types such as SMALLINT data type, INTEGER data type, and BIGINT data type, etc. Each of the integer data types is having a specified range to store the integer data values, we cannot store the data values which are outside the allowed range of the particular data type. PostgreSQL will throw an error or exception if we try to store values outside the defined range of the data type.
Types of PostgreSQL INTEGER
Consider the following table, which illustrates the types of the integer:
Name | Storage Size | Min | Max |
SMALLINT | 2 bytes | -32, 768 | +32, 767 |
INTEGER | 4 bytes | -2, 14, 74, 83, 648 | +2, 14, 74, 83, 647 |
BIGINT | 8 bytes | -92, 23, 37, 20, 36, 85, 47, 70, 000 | +92, 23, 37, 20, 36, 85, 47, 70, 000 |
Examples
Let us consider all types one by one with some examples:
1. SMALLINT
The storage size required for the PostgreSQL SMALLINT data type is 2 bytes. PostgreSQL allows the SMALLINT data type to store values that are within the range of ( -32,767, 32,767 ). The PostgreSQL SMALLINT data type can store 16-bit integer data.
Consider the following example where we can use the PostgreSQL SMALLINT integer data type for storing values such as the count of students and the count of the teacher in a department. Consider the following table named ‘department’, which will store the number of students, and the number of the teacher in a department.
Code:
CREATE TABLE department
(
department_id SERIAL PRIMARY KEY,
department_name VARCHAR ( 255 ) NOT NULL,
students_count SMALLINT NOT NULL CHECK (students_count > 0),
teachers_count SMALLINT NOT NULL CHECK (teachers_count > 0)
);
Explanation: In the above example, we have added students_count and teachers_count columns with data type as the PostgreSQL SMALLINT. We must need at least one student and one teacher to form any department. So we have defined the CHECK constraint on the students_count and teachers_count columns.
2. INTEGER
The storage size required for the PostgreSQL INTEGER data type is 4 bytes. PostgreSQL allows the INTEGER data type to store values that are within the range of ( -2,147,483,648, 2,147,483,647 ) or ( -2^31 to 2^31 -1 (2 Gb) ). The PostgreSQL INTEGER data type is often used as it gives the best performance, range, and storage size. The PostgreSQL INTEGER data type can be used as INT, INTEGER, and INT4. The INTEGER data type can store 32-bit integer data.
Consider the following example where we can use the INTEGER data type for storing values which are big numbers such as the count of students in the university or the count of the teacher in the university.
Consider the table named ‘university’, which will store the number of students and the number of the teacher in a university using the CREATE TABLE statement.
Code:
CREATE TABLE university
(
university_id SERIAL PRIMARY KEY,
university_name VARCHAR ( 255 ) NOT NULL,
students_count INT NOT NULL CHECK (students_count > 0),
teachers_count INTEGER NOT NULL CHECK (teachers_count > 0)
);
Explanation: In the above example, we have added students_count and teachers_count columns with data type as INT.
3. BIGINT
The storage size required for the BIGINT data type is 8 bytes. PostgreSQL allows the BIGINT data type to store values which are within the range of ( -9,223, 372, 036, 854, 775, 808, + 9,223, 372, 036, 854, 775, 807 ) or ( -2^63 to 2^63 -1 ) which means the BIGINT type can store any number. The BIGINT data type is used to store the very big numbers which are out of the storage scope of the PostgreSQL INTEGER data type, such as the unpredictable value storage like memory required by any server. The PostgreSQL BIGINT data type might affect the system’s performance, so we should use BIGINT type if there is a valid reason to use it. The PostgreSQL BIGINT data type can store 64-bit integer data.
Conclusion
We hope from the above article you have understood the PostgreSQL INTEGER data type and the types of the PostgreSQL INTEGER data type such as SMALLINT, INTEGER, and BIGINT, etc. Also, we have added several examples of PostgreSQL INTEGER data types to understand it in detail.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Integer” was beneficial to you. You can view EDUCBA’s recommended articles for more information.