DECIMAL
DECIMAL is a numeric data type in SQL used to store exact numeric values with fixed decimal places. It allows for exact precision, suitable for financial and monetary calculations. The precision and scale can be specified by the user. Precision is the maximum total number of decimal digits to be stored, while scale is the number of digits to the right of the decimal point.
Example
Output
Explanation
Here, DECIMAL(5,2)
is used to store numbers with 5 digits, 2 of which are decimals. This is used in the ‘Price’ column. The output shows the products with their associated prices. With DECIMAL type, calculations are more precise than with FLOAT or DOUBLE. The example creates a ‘Products’ table, inserts rows into it, and then performs a SELECT query to display the contents.
Example
Output
Explanation
In the above code, a table “orders” is created with ‘order_id’, ‘quantity’, and ‘item_price’ columns. The ‘item_price’ column is defined with DECIMAL(5, 2) type, meaning it can store a number with a total of 5 digits, where 2 digits can be after the decimal point. An example row is inserted into the “orders” table, with ‘quantity’ as 5, and ‘item_price’ as 19.99.
Example
Output
Explanation
In the example, the DECIMAL
data type is used to define a column in a table. It is set with precision 10 and scale 2, meaning that it can store numbers with up to 10 digits, two of which are decimals. A record is inserted into the table where the price column is 23.45, which corresponds to the declared DECIMAL(10,2)
type. This means the column ‘Price’ can hold numbers with up to 10 digits, including 2 digits after decimal point.
Example
Output
Explanation
In the above examples, DECIMAL(5,2)
is used to specify a decimal in an Oracle SQL statement. The number (5,2)
after ‘DECIMAL’ represents the precision (total number of digits) and scale (number of digits after the decimal point) of the decimal, respectively. Thus, a decimal with 5 total digits, 2 of which are after the decimal point, is inserted into the table ‘Product’.
Example
Output
Explanation
In this example, a table called Products
is created with two columns: Product_ID
and Product_price
. The Product_price
column is assigned the DECIMAL
data type with a precision of 5 and a scale of 2, which allows us to store numbers with up to 5 digits, up to 2 of them being after the decimal point. A value of 12.99
is then inserted into the Products
table and all rows are selected to display the data inside.