Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PostgreSQL - SMALLINT Integer Data Type
Next article icon

PostgreSQL - SMALLINT Integer Data Type

Last Updated : 08 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In PostgreSQL, the SMALLINT data type is a compact, efficient way to store integer values within a small range. Using only 2 bytes of storage, SMALLINT is ideal for scenarios where the range of possible values is relatively small, such as the age of individuals or the number of pages in a book.

In this article, we will provide a detailed overview of the PostgreSQL SMALLINT data type, including syntax, usage examples, and best practices for storing data within manageable ranges.

PostgreSQL - SMALLINT Integer Data Type

The SMALLINT data type in PostgreSQL stores small-range integer values and is an efficient choice for fields that would not exceed its range. With a storage requirement of only 2 bytes, SMALLINT can store integer values from -32,768 to 32,767, making it suitable for fields like ages, counts, or scores that do not require large storage.

Syntax

variable_name SMALLINT

Examples of PostgreSQL SMALLINT Data Type

Now let's look into some examples of use cases of SMALLINT integer type. These examples illustrate the efficiency of SMALLINT for data with a limited numeric range, making it perfect for cases where storage space and performance are key considerations.

Example 1: Storing Number of Pages in a Book

In this example, we create a table called books to store information about different books, including the number of pages each book has. Using SMALLINT for the pages column is suitable because the number of pages usually falls within the range of SMALLINT.

Query:

CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR (255) NOT NULL,
pages SMALLINT NOT NULL CHECK (pages > 0)
);

INSERT INTO books(title, pages)
VALUES
('Jumanji', 600),
('Insurgent', 7530),
('Nottingham', 8657),
('Dracula', 3000);

SELECT * FROM books;

Output

PostgreSQL SMALLINT Data Type Example

Explanation:

The query will display the records with the number of pages for each book. The pages column is defined as SMALLINT, which is efficient for storing the relatively small values required for book page counts.

Example 2: Storing Age of Students

In this example, we will create a student_age table to store student ages. Age values generally fall within the SMALLINT range, making it an appropriate choice for this data.

Query:

CREATE TABLE student_age(
student_id SERIAL PRIMARY KEY,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
age SMALLINT NOT NULL CHECK (age > 0)
);

INSERT INTO student_age(first_name, last_name, age)
VALUES
('Raju', 'Kumar', 25),
('Nikhil', 'Aggarwal', 21),
('Baccha', 'Yadav', 45),
('Geeta', 'Devi', 30);

SELECT * FROM student_age;

Output

PostgreSQL SMALLINT Data Type Example

Explanation:

The age column uses SMALLINT to efficiently store typical student ages without requiring additional storage space.

Important Points About PostgreSQL SMALLINT Integer Data Type

  • Range Consideration: Ensure that values stored do not exceed the range of SMALLINT to avoid overflow errors.
  • If no constraints are applied, SMALLINT columns can accept any integer value within its range.
  • Ideal Use Cases: Ideal for scenarios where the range of possible values is relatively small, such as ages, counts, or scores.
  • Use Constraints for Data Integrity: Use constraints, like CHECK, to enforce valid data ranges and maintain data integrity.

SMALLINT vs. Other Integer Data Types in PostgreSQL

  1. SMALLINT vs INTEGER: SMALLINT uses 2 bytes of storage, while INTEGER uses 4 bytes and supports a larger range. Use SMALLINT for smaller values to save space, and INTEGER when you need a range of up to approximately ±2 billion.
  2. SMALLINT vs BIGINT: BIGINT occupies 8 bytes and can store very large integers, up to about ±9 quintillion. Choose BIGINT for exceptionally large numbers, such as transaction IDs or account balances, and SMALLINT for more compact data.

Conclusion

The PostgreSQL SMALLINT data type is a practical and efficient choice for storing small-range integer values. By using SMALLINT, we can optimize storage efficiency and ensure data integrity with constraints. Whether storing ages, page numbers, or other limited-range values, SMALLINT offers an ideal balance of compact storage and functionality.


Next Article
PostgreSQL - SMALLINT Integer Data Type

R

RajuKumar19
Improve
Article Tags :
  • PostgreSQL
  • Databases
  • postgreSQL-dataTypes

Similar Reads

    PostgreSQL - INTEGER Data Type
    In PostgreSQL, the INTEGER data type is widely used for storing numerical data efficiently. It is a 4-byte data type that allows us to store whole numbers within a specified range, making it ideal for various use cases like population counts, active user statistics, and more.In this article, we will
    4 min read
    PostgreSQL - Interval Data Type
    The interval data type in PostgreSQL stores time periods using 16 bytes of storage and supports a range from -178,000,000 years to 178,000,000 years. It provides a precision attribute ('p') that allows you to specify the number of fractional digits retained in the seconds field, enhancing the precis
    2 min read
    PostgreSQL - BIGINT Integer Data Type
    PostgreSQL is a powerful open-source relational database management system that offers various integer data types to suit different needs. One of these is the BIGINT data type, designed for storing very large integers. In this article, we will explain the details of the BIGINT data type in PostgreSQ
    3 min read
    PostgreSQL - NUMERIC Data Type
    In PostgreSQL, the NUMERIC data type is designed for high-precision number storage by making it ideal for financial and scientific applications where accuracy is critical. It supports a large number of digits both before and after the decimal point, minimizing rounding errors. Understanding the nuan
    5 min read
    PostgreSQL - TIME Data Type
    In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQL’s TIME data type also supports fractional seconds for u
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences