Introduction to MySQL Decimal
For storing the numeric values we can make the use of integer datatype in MySQL. But when we have to maintain the precision of the numeric values then we have to use the decimal data type that allows us to specify the precision and scale of the decimal numbers that will include a decimal point in its value. This data type is most often used to store the values of amount and weights whose precision and accuracy needs to be maintained.
In this article, we will learn about the decimal datatype, its declaration, attributes supported by it, alternative synonyms for the decimal data type, the storage space required to store the decimal quantities, and how we can use the decimal datatype efficiently and effectively in our database.
Syntax:
The syntax for declaration of the decimal datatype in MySQL table column is as follows –
nameOfColumn DECIMAL(Precision,Scale);
Where nameOf Column is the column name for which you wish to store the data in the decimal datatype.
- DECIMAL is the keyword to declare the decimal datatype which is case insensitive.
- Precision is the number of the total digits in the decimal number to be stored. This value can be between 1 to 65.
- The scale is the parameter that specifies the number of decimal digits that is the number of digits that can be stored after the decimal point. This value should be between 0 to 30 and should always be less than the precision value i.e Scale < Precision.
The synonyms for the DECIMAL datatype are NUMERIC, FIXED, and DEC that can be used alternatively to the DECIMAL keyword.
Attributes:
As with the INTEGER datatype having attributes UNSIGNED and ZEROFILL attribute. Similarly, DECIMAL datatype also has UNSIGNED and ZEROFILL attributes that can be assigned to the decimal values. The UNSIGNED attribute, when assigned to the DECIMAL data, typed column helps us to restrict the storage of negative decimal numbers and makes sure that only positive decimal values are stored in that column.
ZEROFILL attribute when assigned to the decimal datatype column will always display the value of the decimal value by padding the zeroes up to the width of the decimal number declared for that column.
4.5 (5,653 ratings)
View Course
Behavior:
Consider one example where the datatype for the column is defined in the following way –
DECIMAL(5,3) that will allow us to store the values ranging from 99.999 to 99.999 as the scale is 3 the decimal digits after the decimal point can be maximum up to 3 places and the precision is 5 hence, 5-3=2 two digits will be allowed to store in the decimal value of that column. As none of the attributes is specified the negative values that are signed values can also be stored in this field.
Consider one more example in which we don’t specify the scale value, DECIMAL(3). In this case, the default value of scale is considered as 0 and no numeric values containing decimal points can be stored in this column. Only integral numbers can be stored with a range of -999 to 999.
Let us take one more example where we do not specify either of the parameter value i.e precision or scale and declare the datatype of the column as DECIMAL. In this case, the default value of the precision will be considered as 10 and for scale parameter zero value will be considered. Hence, the allowed range of the values to be stored in this column will range from -9999999999 to 9999999999.
Hence, it can be concluded that the precision and scale parameter values are completely optional for specification and the default value of scale is 0 while for precision parameter default value is 10.
DECIMAL Data Type Storage in MySQL
The values for the fractional and integer parts of decimal value that is the value before and after the decimal point are assigned the storage space separately in MYSQL. As the binary format is used to store the values in MYSQL for the decimal data type, it requires 4 bytes of memory to store 9 digits. For each of the packs of 9 digits before and after decimal point 4 bytes of the storage space are required. For the leftover digits for storage, they require the storage space in the following fashion –
1 or 2 leftover digits take 1 byte of storage space, 3 or 4 leftover digits take 2 bytes of storage space, 5 or 6 leftover digits take 3 bytes of storage space and 7 or 9 leftover digits take 4 bytes of storage space.
Consider one example, DECIMAL(20,8) datatype will require 4 bytes of storage space for 8 digits that will occur after the decimal point and for remaining 20-8=12 i.e 12 digits will be split in two. Out of 12, 9 digits will require 4 bytes, and for the remaining 12-9 i.e 3 digits 2 bytes will be required. Hence, in total 4+4+2 =10 bytes of storage space will be required to store the decimal value of this type.
Example
Let us create one table that will contain the decimal data typed column in it. Here, I will create a table named educba_stock containing column cost_in_rupees of decimal datatype with precision 20 and scale 2 using following create query –
CREATE TABLE educba_stock (
identifier INT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(100),
cost_in_rupees DECIMAL(20 , 2 ) NOT NULL
);
that results in the following output –
Let us insert some records in it by executing the following command
INSERT INTO educba_stock(description,cost_in_rupees)
VALUES('Java', 4500.26),('Angular',8512.6),('Mysql',4587.45);
that gives the following output in sqlyog editor –
Let us now retrieve the data stored in educba_stock table using the following select query –
SELECT * FROM educba_stock;
that results in the following output –
Let us alter the column cost_in_rupees and set the attribute ZEROFILL to it using the following ALTER query –
ALTER TABLE educba_stock
MODIFY cost_in_rupees DECIMAL(20,2) ZEROFILL;
that will result in following output –
Note that whenever the ZEROFILL attribute is assigned to DECIMAL datatype, MYSQL automatically adds UNSIGNED attribute to it internally.
Let us retrieve the values from the table and check how they are displayed after assigning ZEROFILL attribute to the decimal datatype of cost_in_rupees column using the following select query –
SELECT * FROM educba_stock;
that now results in the following output –
It can be seen that zeroes are appended and prepended for the maximum display width of that column.
Conclusion
The decimal data type is used to store the numeric values whose precision and accuracy need to be maintained and values after decimal points need to be store. ZEROFILL and UNSIGNED attributes can be used to pad with zeroes for leftover digits and restricting signed negative values to store in your column with decimal datatype.
Recommended Articles
This is a guide to MySQL Decimal. Here we discuss definition, syntax, attributes, DECIMAL Data Type Storage in MySQL, and example with code implementation. You may also have a look at the following articles to learn more –