What Is Real Data Type in PostgreSQL?

//

Scott Campbell

In PostgreSQL, the REAL data type is used to store single-precision floating-point numbers. It is a 4-byte data type that can represent a wide range of values, including both positive and negative numbers.

Working with REAL Data Type

To define a column with the REAL data type in PostgreSQL, you can use the following syntax:

CREATE TABLE table_name (
    column_name REAL
);

You can also specify the precision of the REAL data type using the syntax:

CREATE TABLE table_name (
    column_name REAL(precision)
);

The precision parameter specifies the maximum number of digits that can be stored in the column. The default precision is 6 if not specified.

Examples

Let’s take a look at some examples to understand how the REAL data type works:

  • Create a table named “products” with a column “price” of type REAL:
  •     CREATE TABLE products (
            price REAL
        );
      
  • Insert some sample data into the table:
  •     INSERT INTO products (price) VALUES (10.99);
        INSERT INTO products (price) VALUES (-5.75);
      
  • Select all rows from the “products” table:
  •     SELECT * FROM products;
      
  • The result would be:
  • price
    10.99
    -5.75

Comparison and Arithmetic Operations

The REAL data type also supports comparison and arithmetic operations, just like other numeric data types in PostgreSQL.

Here are some examples:

  • Comparison:
  •     SELECT * FROM products WHERE price > 0;
        SELECT * FROM products WHERE price < 0;
        SELECT * FROM products WHERE price = 10.99;
      
  • Arithmetic Operations:
  •     SELECT price + 5.5 FROM products;
        SELECT price - 2.25 FROM products;
        SELECT price * 2 FROM products;
        SELECT price / 3 FROM products;
      

Conclusion

The REAL data type in PostgreSQL is a useful way to store single-precision floating-point numbers. It allows you to represent a wide range of values and perform various operations on them. By understanding how to work with the REAL data type, you can effectively manage numeric data in your PostgreSQL database.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy