REAL
REAL is a data type in SQL used to store floating point numbers with 7 digits of precision. It can be used to save space when the higher precision provided by DOUBLE is not necessary. Being a floating point type, it is used to represent fractional numbers and can handle exponential values as well.
Example
CREATE TABLE Inventory ( ItemID INT AUTO_INCREMENT, ItemName VARCHAR(100) NOT NULL, ItemWeight REAL NOT NULL, PRIMARY KEY (ItemID));
INSERT INTO Inventory (ItemName, ItemWeight)VALUES ('Widget', 1.5), ('Gadget', 0.75);
SELECT * FROM Inventory;Output
+--------+----------+------------+| ItemID | ItemName | ItemWeight |+--------+----------+------------+| 1 | Widget | 1.5 || 2 | Gadget | 0.75 |+--------+----------+------------+Explanation
In this example, we have a SQL table named Inventory with three fields: ItemID, ItemName and ItemWeight. ItemID is an integer, ItemName is a text string, and ItemWeight is a REAL data type. The REAL data type is a floating-point number with seven digits of precision that can accept data values from -3.4E38 to 3.4E38.
In the INSERT INTO command, we insert two rows of data into the Inventorytable. The ItemWeight for each row is expressed as a REAL data value. Using the SELECT command, we display the contents of the Inventory table.
Example
CREATE TABLE test_table ( column1 REAL);
INSERT INTO test_table (column1) VALUES (123.45);
SELECT * FROM test_table;Output
column1--------- 123.45(1 row)Explanation
In the example, a new table test_table is created with one column column1 with the data type REAL. The REAL data type in PostgreSQL is used to store single precision floating point numbers. A value of 123.45 is then inserted into column1. Finally, all data from test_table is selected, which in this case shows the inserted 123.45.
Example
DECLARE @RealValue REAL;SET @RealValue = 1234.5678;SELECT @RealValue;Output
1234.5678Explanation
The example demonstrates how to declare a REAL data type variable @RealValue, set a value to it, and then select the value. The REAL data type is a floating point number data type that can be used to store fractional numbers with up to seven digits of precision.
Example
CREATE TABLE Employee ( ID INT PRIMARY KEY NOT NULL, Salary REAL NOT NULL);INSERT INTO Employee (ID, Salary)VALUES (1, 24.5), (2, 37.8), (3, 30.2);SELECT * FROM Employee;Output
ID | Salary----------------1 | 24.52 | 37.83 | 30.2Explanation
In the code example, a new table ‘Employee’ is created with ‘ID’ and ‘Salary’ columns. The ‘Salary’ column uses the REAL datatype, which is designed to store decimal or floating point values in SQLite. Three rows of data are then inserted into the table using the INSERT INTO statement. Finally, the SELECT statement is used to fetch and display all data from the ‘Employee’ table. The output shows the values of ‘ID’ and ‘Salary’ columns from all rows.