postgresql
  1. postgresql-numeric

Numeric - (PostgreSQL Data Types)

Numeric is a PostgreSQL data type used to store arbitrary-precision numbers. In this tutorial, we'll explore the syntax, example, output, explanation, use, and important points of the Numeric data type in PostgreSQL.

Syntax

NUMERIC(precision, scale)
  • precision: The total number of digits that can be used to represent the number (e.g. 5).
  • scale: The number of digits to the right of the decimal point (e.g. 2).

Example

CREATE TABLE sales (
    price NUMERIC(5,2)
);

INSERT INTO sales (price) VALUES (12.50);
INSERT INTO sales (price) VALUES (7.25);

SELECT * FROM sales;

The output of this query would be:

price
------
12.50
7.25

Explanation

In this example, we created a table called "sales" with one column called "price". The "price" column is of the Numeric data type with a precision of 5 and a scale of 2, meaning it can store up to 5 digits in total with up to 2 decimals to the right of the decimal point.

We then inserted two values into the "price" column and retrieved all rows from the "sales" table.

Use

The Numeric data type is useful for storing monetary data or any other values that require a high degree of precision. It can store very large or very small numbers with a high degree of accuracy.

Important Points

  • The maximum value that can be stored in a Numeric column is 10^131072.
  • The minimum value that can be stored in a Numeric column is -10^131072.
  • The maximum value for the "precision" parameter is 1000.
  • The maximum value for the "scale" parameter is equal to the "precision" parameter.
  • Numeric values are stored in a variable-length format, meaning that they take up only as much space as necessary.

Summary

In this tutorial, we discussed the Numeric data type in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the Numeric data type. With this knowledge, you can now use the Numeric data type to store arbitrary-precision numbers in your PostgreSQL databases.

Published on: