EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Integer

PostgreSQL Integer

By Sohel SayyadSohel Sayyad

PostgreSQL Integer

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

  1. PostgreSQL Variables
  2. PostgreSQL Commands
  3. Procedures in PostgreSQL
  4. Ansible Variables
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more