PostgreSQL stores information in the form of tables and to use and modify the table data we will have to create them first. While creating tables, we need to define their data types also. These data types are necessary to store the specific data in the table columns. These data types include numeric data types, time and date data types, character data types, boolean data types, etc. This post will cover a numeric data type that is used to store single-precision floating-point numbers basically 32-bit in size.
Let’s see how to use these REAL data types in PostgreSQL.
How to Use REAL Data Type in PostgreSQL?
The REAL data type is actually a floating-point type that stores the single-precision floating-point numbers. The size of these data types is 32-bit, which means that it uses comparatively less storage as compared to the DOUBLE PRECISION data type. However, the precision of this data type is also less as compared to the DOUBLE PRECISION data type. So if we wish to have more precision, you can go for the DOUBLE PRECISION data type.
However, the REAL data type is extensively used in the fields of engineering, finance, economics, etc. to store the numeric values containing the floating point.
Now let’s see how we declare and use the REAL data type in PostgreSQL using an example.
Example: How to Use REAL Data Type in PostgreSQL?
Consider creating a table named “Velocity” having a “velocity_value” column which is of REAL data type. The query can be written as:
CREATE TABLE Velocity( velocity_value REAL );
Upon the execution of the above query, a table will be created. Now we will insert some values in the table by the following query:
INSERT INTO Velocity(velocity_value) VALUES (784.93); INSERT INTO Velocity(velocity_value) VALUES (256.87);
The values will be inserted in the table successfully which can be ensured by the following output:
Execute the SELECT statement to get the table:
We can see that the data type of the column is REAL data type. This is how we use the REAL data type in this database.
Conclusion
The REAL data type is a numeric data type used to store the single-precision floating point numbers. This data type requires fewer storage constraints as compared to the DOUBLE PRECISION data type. It can be storage efficient at times but they offer less precision. If we wish to have high precision, we can use DOUBLE PRECISION data types. In this post, we have discussed how to use the REAL data type in PostgreSQL using examples.