PostgreSQL NumericIn this section, we are going to understand the working of PostgreSQL Numeric data types, which allows us to store the numeric data. And we also see examples of Numeric data type. What is PostgreSQL NUMERIC Data Type?In PostgreSQL, the Numeric data type is used to store the numbers with various significant numbers of digits. In other words, we can say that the PostgreSQL Numeric data type is used to specify the numeric data into the table, which needs quantities or monetary amounts where the precision is required. The Numeric data types contain the following:
The below table contains all Numeric data types that support in PostgreSQL:
Syntax of PostgreSQL Numeric data typeThe syntax of the PostgreSQL Numeric data type is as follows: In the above syntax, we have used the following parameters:
We can understand the concept of precision and scale by seeing in the following example: Suppose we have the number 2356.78. In this number, the precision is 6, and the scale is 2. Note: In PostgreSQL, the Numeric data type can have a value of up to 131,072 digits before the decimal point of 16,383 digits after the decimal point. The Numeric data type scale can be Positive (+) Or Zero (0).The below syntax displays the Numeric data type with Scale ZERO: And if we ignore both precision and scale, we can have any precision and scale up to the limit of the precision and scale, which mentioned above. Note: In PostgreSQL, the Numeric and Decimal types are corresponding to each other, and both of them also follow the SQL standard.If we don't require the precision, we cannot use the Numeric data type since the calculations on Numeric values are usually slower than double precisions, floats, and integers. Examples of PostgreSQL Numeric data typesLet us see different examples to understand how the PostgreSQL Numeric data type works. Example of Storing numeric valuesThe PostgreSQL will round the value to a defined number of fractional digits if we want to store a value more extensive than the declared scale of the Numeric column. For this, we will create one new table name Items table with the help of the CREATE command and insert some values by using the INSERT command. Here, we also use the drop command to drop the table if it exists in a particular database. We are going to create Items tables by using the CREATE command into the Organization database: Output The Items table has been successfully created after executing the above commands, as shown in the below screenshot: Once the Items table has been generated, we are ready to insert some items into it by using the INSERT command with their prices whose scales exceed the scale declared in the item_price column: Output After implementing the above command, we will get the following output, which displays that the value has been inserted successfully into the Items table. Because the scale of the item_price column is 2, PostgreSQL rounds the values 300.512,300.513. 300.514 up to 300.51. After creating and inserting the Items table's values, we will use the SELECT command that returns all rows of the Items table: Output After executing the above command, we will get the following result: If we want to store a value whose precision surpasses the declared precision, then the PostgreSQL will show an error as we can see in the below example: Output On executing the above command, we will get the following error that the numeric field overflow, which implies that a field with precision 5 and scale 2 must round to absolute value, which is less than 10^3. Example of PostgreSQL NUMERIC data type and NaNLet see on sample example of PostgreSQL Numeric data type and NaN. If we want to fetch the numeric values, then the PostgreSQL NUMERIC data type can also have a special value called NaN, and the NaN stand for not-a-number In the below example, we will update the item_price of the item_id 1 to NaN: Output Once we implement the above command, we will get the below message window, which displays that the particular value has been updated successfully into the Items table. Note: In the above Update statement, we have used the single quotes ('') to enclose the NaN.Now we have used the SELECT command to fetch all the records from the Items table: Output After executing the above statement, we will get the below result: Generally, the NaN is not equal to any number, including itself, which means that the following expression NaN = NaN will return FALSE. But, the two NaN values can be equivalent, and NaN is more significant than other numbers. This execution allows PostgreSQL to categorize the Numeric values and use them in tree-based indexes. To categorize the Items based on their Item_prices, we will use the below command: Output On executing the above command, we will get the following output: As we can see in the above screenshot that the NaN is greater than the value 300.51. OverviewIn the PostgreSQL Numeric data type section, we have learned the following topics:
Next TopicPostgreSQL Character |