EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MySQL Decimal

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » MySQL Tutorial » MySQL Decimal

MySQL Decimal

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Popular Course in this category
MS SQL Training (13 Courses, 11+ Projects)13 Online Courses | 11 Hands-on Projects | 62+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,653 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)

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 –

MySQL Decimal 1

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 –

MySQL Decimal 2

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 –

MySQL Decimal 3

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 –

MySQL Decimal 4

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 –

snapshot

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 –

  1. MySQL ROUND
  2. MySQL BigInt
  3. MySQL Update Set
  4. MySQL Merge

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL count()
    • MIN() in MySQL
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • TRUNCATE TABLE MySQL
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL 
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT 
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL COALESCE
    • MySQL Cluster
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • Introduction to MySQL
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • How To Install MySQL
    • MySQL OpenSource
    • MySQL Commands
    • Views in MySQL
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL UUID
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL Transaction
    • MySQL sort_buffer_size
    • MySQL Sync
    • MySQL Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MS SQL Certification Courses Learn More