Introduction to SQLite REAL Data Type
REAL
is a data type in SQLite used to store floating-point numbers, which are numbers with a decimal point. It is one of the most commonly used data types in SQLite and can store both single-precision and double-precision floating-point numbers, with high precision and variable precision characteristics.
Syntax
The REAL
data type can be used to declare columns or variables, and its syntax is as follows:
column_name REAL
Use Cases
The REAL
data type is typically used to store data related to scientific calculations, geographical locations, temperatures, and other data with low precision requirements. It is also suitable for scenarios where it is necessary to store currency amounts with multiple decimal places.
Compared to integer types, the REAL
data type can store larger values and higher precision, making it a better choice for high-precision floating-point calculations or more accurate decimal representations.
Examples
Here are two examples of using the REAL
data type.
Example 1
Assuming there is a table used to store students’ height information with the following table structure:
CREATE TABLE student (
id INTEGER PRIMARY KEY,
name TEXT,
height REAL
);
Now, we need to insert a height information into the table, where the height is 1.75 meters. The SQL statement would be as follows:
INSERT INTO student (id, name, height) VALUES (1, 'Tom', 1.75);
To query all the height information of students in the table, the SQL statement would be as follows:
SELECT * FROM student;
The query result would be:
id | name | height |
---|---|---|
1 | Tom | 1.75 |
Example 2
Assuming there is a table used to store product price information with the following table structure:
CREATE TABLE product (
id INTEGER PRIMARY KEY,
name TEXT,
price REAL
);
Now, we need to insert a product information into the table, where the price is 19.99 US dollars. The SQL statement would be as follows:
INSERT INTO product (id, name, price) VALUES (1, 'iPhone case', 19.99);
To query all the price information of products in the table, the SQL statement would be as follows:
SELECT * FROM product;
The query result would be:
id | name | price |
---|---|---|
1 | iPhone case | 19.99 |
Conclusion
The REAL
data type is a commonly used data type in SQLite for storing floating-point numbers. It is suitable for storing data related to scientific calculations, geographical locations, temperatures, and other data with low precision requirements, as well as currency amounts with multiple decimal places. Using the REAL
data type ensures the precision and accuracy of the data, making it particularly suitable for storing decimal representations with higher precision.