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.
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 INTO products (price) VALUES (10.99); INSERT INTO products (price) VALUES (-5.75);
SELECT * FROM products;
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:
SELECT * FROM products WHERE price > 0; SELECT * FROM products WHERE price < 0; SELECT * FROM products WHERE price = 10.99;
SELECT price + 5.5 FROM products; SELECT price - 2.25 FROM products; SELECT price * 2 FROM products; SELECT price / 3 FROM products;
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.