0
0
PostgreSQLquery~10 mins

Integer types (smallint, integer, bigint) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer types (smallint, integer, bigint)
Choose integer type
smallint: 2 bytes, range -32768 to 32767
integer: 4 bytes, range -2147483648 to 2147483647
bigint: 8 bytes, range -9223372036854775808 to 9223372036854775807
Store value in chosen type
Use value in queries/calculations
You pick one of three integer types based on size and range, then store and use values accordingly.
Execution Sample
PostgreSQL
CREATE TABLE numbers (
  small_num SMALLINT,
  int_num INTEGER,
  big_num BIGINT
);

INSERT INTO numbers VALUES (32000, 2000000000, 9000000000000000000);
Creates a table with three integer columns of different sizes and inserts values within their ranges.
Execution Table
StepActionData TypeValue InsertedResult
1Create table with smallint columnsmallintN/AColumn created with 2 bytes storage
2Create table with integer columnintegerN/AColumn created with 4 bytes storage
3Create table with bigint columnbigintN/AColumn created with 8 bytes storage
4Insert 32000 into smallintsmallint32000Success, within range (-32768 to 32767)
5Insert 2000000000 into integerinteger2000000000Success, within range (-2147483648 to 2147483647)
6Insert 9000000000000000000 into bigintbigint9000000000000000000Error: value out of range for bigint
7Attempt to insert 40000 into smallintsmallint40000Error: value out of range for smallint
8Attempt to insert 3000000000 into integerinteger3000000000Error: value out of range for integer
9Insert -9000000000000000000 into bigintbigint-9000000000000000000Error: value out of range for bigint
10Query all valuesN/AN/AReturns inserted rows with correct values
💡 Execution stops after all inserts and queries; errors occur when values exceed type ranges.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 9Final
small_numNULL3200032000320003200032000
int_numNULLNULL2000000000200000000020000000002000000000
big_numNULLNULLNULLNULLNULLNULL
Key Moments - 3 Insights
Why does inserting 40000 into smallint cause an error?
Because smallint can only store values from -32768 to 32767, and 40000 is outside this range (see execution_table step 7).
Can integer store values larger than 2 billion?
No, integer's max is 2147483647, so values like 3000000000 cause errors (see execution_table step 8).
Why use bigint instead of integer or smallint?
Bigint stores much larger numbers (8 bytes), useful when values exceed integer or smallint ranges (see execution_table steps 6 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value is stored in small_num after step 4?
A32000
B40000
CNULL
D-32768
💡 Hint
Check the 'Value Inserted' and 'Result' columns at step 4 in the execution_table.
At which step does inserting a value cause an error due to exceeding integer range?
AStep 5
BStep 8
CStep 7
DStep 9
💡 Hint
Look for 'Error' in the 'Result' column related to integer type in execution_table.
If you tried to insert 50000 into smallint, what would happen?
AIt would store as 32767
BIt would store successfully
CIt would cause an error
DIt would store as NULL
💡 Hint
Refer to the error shown in execution_table step 7 for out-of-range smallint values.
Concept Snapshot
Integer types in PostgreSQL:
- smallint: 2 bytes, range -32768 to 32767
- integer: 4 bytes, range -2147483648 to 2147483647
- bigint: 8 bytes, range -9223372036854775808 to 9223372036854775807
Choose type based on needed range to avoid errors.
Full Transcript
This visual execution shows how PostgreSQL handles integer types: smallint, integer, and bigint. Each type has a specific size and range. We create a table with columns of each type and insert values within their ranges successfully. Attempts to insert values outside the allowed range cause errors. The variable tracker shows how values are stored step by step. Key moments clarify why errors happen when values exceed type limits. The quiz tests understanding of value storage and errors related to integer types.