What if your database could save space and avoid errors just by choosing the right number size automatically?
Why Integer types (smallint, integer, bigint) in PostgreSQL? - Purpose & Use Cases
Imagine you are keeping track of the number of books in a library using a simple list on paper. You write down numbers without thinking about how big or small they might get.
Later, you realize some numbers are too big to fit in your small boxes, and some boxes are wasted because the numbers are tiny.
Using the same size box for all numbers wastes space or causes errors when numbers are too big.
Manually guessing the right size for each number is slow and can lead to mistakes, like running out of space or using too much memory.
Integer types like smallint, integer, and bigint let the database pick the right size box automatically.
This saves space and avoids errors by matching the number size to the right type.
CREATE TABLE books (count INTEGER); -- always uses 4 bytesCREATE TABLE books (count SMALLINT); -- uses 2 bytes for small numbers
Efficient storage and accurate handling of numbers of different sizes without wasting space or risking errors.
A bank stores account balances: smallint for small accounts, integer for regular accounts, and bigint for very large balances, ensuring fast and safe data handling.
Integer types help match number size to storage space.
This prevents wasted space and errors from numbers too big or too small.
Choosing the right integer type improves database efficiency and reliability.
Practice
Solution
Step 1: Understand integer types and their sizes
smallintstores small numbers,integerstores medium-sized numbers, andbigintstores very large numbers.Step 2: Identify the type for very large whole numbers
Since the question asks for very large whole numbers,bigintis the correct choice.Final Answer:
bigint -> Option AQuick Check:
Very large whole numbers = bigint [OK]
- Confusing smallint with bigint
- Choosing decimal which is for decimals, not integers
- Picking integer which is medium size
age as a 2-byte integer in PostgreSQL?Solution
Step 1: Recall PostgreSQL integer type syntax
PostgreSQL usesSMALLINTfor 2-byte integers. The syntax is simplycolumn_name SMALLINT;.Step 2: Evaluate each option
age SMALLINT;uses correct syntax. The other options incorrectly use size in parentheses (like INT2(2), INTEGER(2), BIGINT(2)), which is invalid for integer types in PostgreSQL.Final Answer:
age SMALLINT; -> Option DQuick Check:
2-byte integer = SMALLINT syntax [OK]
- Adding size in parentheses like INTEGER(2)
- Using BIGINT for small numbers
- Specifying size like INT2(2) for integer types
CREATE TABLE test_numbers (num SMALLINT); INSERT INTO test_numbers VALUES (40000); SELECT * FROM test_numbers;
Solution
Step 1: Understand SMALLINT range
SMALLINTcan store values from -32768 to 32767 only.Step 2: Check inserted value against range
40000 is greater than 32767, so inserting it causes an error.Final Answer:
Error: integer out of range -> Option BQuick Check:
40000 > SMALLINT max 32767 = error [OK]
- Assuming value truncates silently
- Expecting 0 or NULL instead of error
- Confusing SMALLINT range with INTEGER
CREATE TABLE sales (id INTEGER, amount SMALLINT);
But when inserting a large amount like 40000, you get an error. How do you fix it?
Solution
Step 1: Identify cause of error
Amount column isSMALLINTwhich maxes at 32767, so 40000 is too large.Step 2: Choose appropriate type to hold 40000
INTEGERcan hold values up to 2,147,483,647, so changingamounttoINTEGERfixes the error.Final Answer:
Change amount column type to INTEGER -> Option AQuick Check:
40000 fits INTEGER but not SMALLINT [OK]
- Changing unrelated column type
- Trying to insert smaller values only
- Choosing
tinyintwhich is a MySQL type, not PostgreSQL
Solution
Step 1: Understand size limits of integer types
smallintmax is 32767,integermax is about 2,147,483,647,bigintsupports much larger numbers.Step 2: Match requirement to type
User IDs may exceed 2 billion, sointegeris insufficient.bigintis needed to store larger values.Final Answer:
bigint because it supports numbers larger than 2 billion -> Option CQuick Check:
IDs > 2 billion need bigint [OK]
- Choosing smallint for large IDs
- Assuming integer supports >2 billion
- Confusing serial with integer size
Ready to prove it?
Pass the test and this pattern moves unknown
