Learn how to use PostgreSQL numeric data types, including SMALLINT, INTEGER, BIGINT, NUMERIC, DECIMAL, REAL, and DOUBLE PRECISION, with practical SQL examples.
Numeric data types are used to store numbers in a database. These numbers may be whole numbers or decimal numbers.
For example:
PostgreSQL provides different numeric data types depending on the size, accuracy, and type of numbers we want to store.
Simple definition:
A numeric data type specifies how PostgreSQL stores and processes numerical values.
PostgreSQL numeric data types are divided into three main groups.
1. Whole Number Types (Integer Types)
2. Exact Decimal Types
3. Approximate Decimal Types
Integer data types are used to store whole numbers without decimal points.
For example:
PostgreSQL provides three integer data types.
| Data type | Storage | Range |
|---|---|---|
| SMALLINT | 2 bytes | -32,768 to 32,767 |
| INTEGER | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| BIGINT | 8 bytes | Approximately -9.22 quintillion to +9.22 quintillion |
Understanding the differences
| Information | Suitable data type |
|---|---|
| Student age | SMALLINT |
| Number of students | INTEGER |
| Large transaction counter | BIGINT |
CREATE TABLE students (
student_id INTEGER,
age SMALLINT,
total_marks INTEGER,
registration_number BIGINT
);
Important: INTEGER cannot store a decimal fraction.
For example, if we try:
INSERT INTO students (student_id, age)
VALUES (1, 20.5);
PostgreSQL will return an error because 20.5 is not an integer value.
NUMERIC and DECIMAL are used to store exact decimal numbers.
For example:
They are particularly useful when accuracy is important, such as financial calculations.
NUMERIC(precision, scale)
Here:
semester_fee NUMERIC(10,2)
It means:
Understanding NUMERIC(10,2)
Digits before decimal
Digits after decimal
Total precision = 10 digits
CREATE TABLE fee_records (
student_id INTEGER,
semester_fee NUMERIC(10,2)
);
Insert records:
INSERT INTO fee_records
VALUES
(1, 45000.50),
(2, 55000.75),
(3, 60000.00);
View records:
SELECT * FROM fee_records;
Note: DECIMAL and NUMERIC are equivalent in PostgreSQL. Both provide exact numeric storage.
This is one of the most important differences students should understand.
Consider a university fee record.
Can we use INTEGER for both columns?
No. INTEGER is designed for whole numbers, whereas NUMERIC can store exact decimal values.
| Feature | INTEGER | NUMERIC |
|---|---|---|
| Stores whole numbers | Yes | Yes |
| Stores decimal values | No | Yes |
| Accuracy | Exact whole numbers | Exact decimal numbers |
| Storage | Fixed 4 bytes | Variable |
| Decimal precision | Not applicable | Can be specified |
| Best use | Age, count, quantity | Fee, salary, percentage |
INTEGER vs NUMERIC
INTEGER
Whole number
NUMERIC(10,2)
Decimal number
Example SQL
CREATE TABLE university_fees (
student_id INTEGER,
semester_fee NUMERIC(10,2)
);
Here, student_id stores a whole number, while semester_fee stores an exact decimal amount.
Yes!
NUMERIC can store both whole numbers and decimal numbers.
For example:
SELECT
100::INTEGER AS integer_value,
100::NUMERIC AS numeric_value;
Both values are valid.
Remember:
INTEGER = Whole numbers
NUMERIC = Whole numbers + Decimal numbers (with exact precision).
REAL and DOUBLE PRECISION are used to store approximate decimal numbers.
Unlike NUMERIC, these data types do not guarantee exact decimal representation.
| Data type | Storage | Approximate precision |
|---|---|---|
| REAL | 4 bytes | 6 decimal digits |
| DOUBLE PRECISION | 8 bytes | 15 decimal digits |
CREATE TABLE measurements (
temperature REAL,
scientific_value DOUBLE PRECISION
);
When should we use them?
| Data type | Category | Example |
|---|---|---|
| SMALLINT | Integer | 100 |
| INTEGER | Integer | 25000 |
| BIGINT | Integer | 9000000000 |
| NUMERIC | Exact decimal | 45000.50 |
| DECIMAL | Exact decimal | 125.75 |
| REAL | Approximate decimal | 3.14159 |
| DOUBLE PRECISION | Approximate decimal | 3.1415926535 |
Create a university student table using appropriate numeric data types.
Task: Select suitable data types for these columns.
| Column | Example value |
|---|---|
| Student ID | 1001 |
| Age | 22 |
| Semester Fee | 45000.50 |
| CGPA | 3.75 |
| Total Marks | 850 |
| Temperature | 36.5 |
Suggested SQL solution:
CREATE TABLE student_records (
student_id INTEGER,
age SMALLINT,
semester_fee NUMERIC(10,2),
cgpa NUMERIC(3,2),
total_marks INTEGER,
temperature REAL
);
MCQ Assessment
6 Questions
Q1. Which data type stores whole numbers?
A. NUMERIC
B. INTEGER
C. REAL
D. DOUBLE PRECISION
Q2. Which data type is best for an exact fee amount of 45000.50?
A. INTEGER
B. REAL
C. NUMERIC(10,2)
D. SMALLINT
Q3. How many bytes does INTEGER use?
A. 2 bytes
B. 4 bytes
C. 8 bytes
D. 16 bytes
Q4. What does NUMERIC(8,2) mean?
A. 8 digits after decimal
B. 2 total digits
C. 8 total digits, 2 after decimal
D. 8 bytes
Q5. Which data type stores approximate decimal values?
A. INTEGER
B. NUMERIC
C. REAL
D. BIGINT
Q6. Can NUMERIC store a whole number?
A. No
B. Yes
C. Only negative numbers
D. Only zero
Submit Answers
Official documentation: PostgreSQL Numeric Types