21.6 C
Jaipur
Monday, October 18, 2021

Using Floating Data Types in PostgreSQL

Must read

PostgreSQL supports multiple different data types. One such data type is the floating data type which is used to store the floating-point data. We all understand that the handling and manipulation of this particular data type is a bit tricky and complex, so the programmers find it a little difficult to use it. Therefore, in this guide, we have decided to share the method of using the floating data types in PostgreSQL in Windows 10 after sharing with you a brief introduction of this data type.

What are Floating Data Types in PostgreSQL?

The floating data type in any programming language is used to store data in the form of decimal points. However, as far as PostgreSQL is concerned, its floating data type is further divided into three different types, i.e. Float, Real, and Numeric. We will be explaining to you all of these three types in the sections of this article below. These types of floating data can be chosen according to the needs of the data you want to store or manipulate.

First Type: The Float Type:

This data type can be used to store data of up to 8 bytes. You need to specify the number of bytes after the “Float” keyword while declaring a data type variable. The number of bytes that you will specify with this keyword will decide which values can be stored into this data type.

Second Type: The Real Type:

This data type can hold 4 bytes of floating data correct up to six decimal places. The “Real” data type can also be replaced with the “Float4” data type since they both refer to the same data capacity, i.e. 4 bytes.

Third Type: The Numeric Type:

This data type accepts two different arguments, i.e. p and s. “p” represents the digits in this notation, whereas “s” represents the number after the decimal point.

Why should we use the Floating Data Type?

As we have already mentioned, the floating data type is used to store the decimal point data; therefore, we can use it to store the data that is not in the normal integer type. We know that we perform certain numeric calculations such as division etc., in which the results that we get can be in the decimal point number. Such an output cannot be stored into the regular integer data type. For that, we need to use floating data type in PostgreSQL in Windows 10. Apart from that, we can also create PostgreSQL tables with the data that belongs to this particular data type.

Method of Using the Floating Data Types in PostgreSQL in Windows 10:

For learning the usage of the different floating data types, i.e. Float, Real, and Numeric in PostgreSQL in Windows 10, you will have to go through the following procedure:

Step # 1: Creating a PostgreSQL Table for the Floating Data Types in Windows 10:

First, we want to create a PostgreSQL table that can hold the different floating data types. We will create a single table that can hold all three types of floating data by running the query stated below:

# CREATE TABLE floatingDataTypes (FloatNum float8 NOT NULL, RealNum real NOT NULL, NumericNum NUMERIC (3, 3) NOT NULL);

In this query, we have created a PostgreSQL table named floatingDataTypes with three attributes, i.e. FloatNum, RealNum, and NumericNum, for the float, real, and numeric types of floating data, respectively. Here, we have used the float8 data type; however, you can also choose the float4 data type.

After executing this query, you will get the “CREATE TABLE” response on the console, which will confirm the creation of this table as displayed in the below image:

Step # 2: Inserting the Floating Values in the Newly Created Table:

After creating the floatingDataTypes table, we will try to insert some values into each of these three attributes or types of floating data. We will insert three different records to this table with the help of the query cited below:

# INSERT INTO floatingDataTypes VALUES(5638358295682736, 5373274212, 0.36), (5365842323231243, 7652752893, 0.64), (1625783829122541, 5627842862, 0.22);

With the help of this query, we have inserted three different records into the floatingDataTypes table according to the types of attributes specified within that table.

The “INSERT 0 3” response will indicate a successful insertion of these records, as shown in the following image:

Step # 3: Viewing if the Floating Values have been inserted correctly or not:

Now, we will view these records to see if the different types of floating data have been inserted correctly into our PostgreSQL table or not. For that, we will run the query given below:

# SELECT * FROM floatingDataTypes;

The following table representation in the output verifies that the different floating data types have been inserted successfully into our floatingDataTypes table. You can see from the following output that all the records have been inserted in the form of floating numbers according to the specific type provided in the PostgreSQL table attribute.

Step # 4: Performing any Test Operation on the inserted Floating Values (Optional):

Up till now, we have successfully managed to use all the different types of floating data by inserting it into a PostgreSQL table. Now, this step is totally optional. We just wanted to show you how you can perform any test operation on this floating data. The PostgreSQL query shown below depicts this:

# SELECT avg(FloatNum), avg(RealNum), avg(NumericNum) FROM floatingDataTypes;

In this query, we have used an aggregate function of PostgreSQL in Windows 10, i.e. the average function (avg). This function takes a PostgreSQL table column as itsonly argument and then processes it to calculate the average of the values of that particular column. We will run this aggregate function thrice in our query to calculate the averages of all the  three attributes or columns of our floatingDataTypes PostgreSQL table.

As a result of running this query, the average values of all the three columns of our floatingDataTypes table, i.e. FloatNum, RealNum, and NumericNum, will be displayed on the console as shown in the following image:

Conclusion:

In this guide, we explained to you the thorough procedure of using the floating data types in PostgreSQL in Windows 10. We first gave you a quick introduction of this data type, followed by explaining the different categories into which this data type is further divided. After that, we shared with you a comprehensive example in which we used all the three types of floating data by creating a PostgreSQL table and inserting some values into it. Finally, we also used an aggregate function to process the records inserted into our PostgreSQL table. With the help of this article, you will be able to use the different categories of floating data types in PostgreSQL in Windows 10 very effectively.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article