0
0
MySQLquery~10 mins

Decimal and floating-point types in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Decimal and floating-point types
Start: Define column type
Choose DECIMAL or FLOAT/DOUBLE
Store value with precision
Retrieve value
Observe exactness or approximation
Use in calculations
End
This flow shows how MySQL stores and retrieves decimal and floating-point numbers, highlighting exactness for DECIMAL and approximation for FLOAT/DOUBLE.
Execution Sample
MySQL
CREATE TABLE prices (
  price_decimal DECIMAL(5,2),
  price_float FLOAT
);

INSERT INTO prices VALUES (123.45, 123.45);
SELECT * FROM prices;

UPDATE prices SET price_decimal = 0.1, price_float = 0.1;
SELECT * FROM prices;
This code creates a table with DECIMAL and FLOAT columns, inserts 123.45 into both, selects to show storage, updates to 0.1, and selects again to demonstrate exact vs approximate storage.
Execution Table
StepActionInput ValueStored Value in DECIMALStored Value in FLOATNote
1Create table with DECIMAL(5,2) and FLOAT columnsN/AN/AN/ATable schema defined
2Insert value 123.45 into both columns123.45123.45123.44999694824219FLOAT stores approximate value
3Select values from tableN/A123.45123.44999694824219DECIMAL exact, FLOAT approximate
4Update both columns to 0.10.10.100.10000000149011612FLOAT cannot store 0.1 exactly
5Select values from tableN/A0.100.10000000149011612Shows difference in precision
6EndN/AN/AN/ADemonstrated exact vs approximate storage
💡 Execution ends after showing how DECIMAL stores exact values and FLOAT stores approximate values.
Variable Tracker
VariableStartAfter Insert 123.45After Update 0.1Final
price_decimalNULL123.450.100.10
price_floatNULL123.449996948242190.100000001490116120.10000000149011612
Key Moments - 3 Insights
Why does the FLOAT column show a slightly different number than what was inserted?
FLOAT stores numbers approximately using binary floating-point, so some decimal numbers like 0.1 cannot be represented exactly, as shown in execution_table rows 2 and 4.
Why does DECIMAL store the exact number I inserted?
DECIMAL stores numbers as strings of digits with fixed precision, preserving exact decimal values, as seen in execution_table rows 2 and 4.
What happens if I insert a number with more decimal places than DECIMAL(5,2) allows?
MySQL rounds or truncates the number to fit the defined precision, ensuring stored values match the DECIMAL scale, which is why we see 0.10 instead of 0.1 in row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the stored value in the FLOAT column after inserting 123.45?
A123.45 exactly
B123.00
CApproximately 123.44999694824219
DNULL
💡 Hint
Check the 'Stored Value in FLOAT' column at step 2 in the execution_table.
At which step does the DECIMAL column store the value 0.10?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Stored Value in DECIMAL' column for step 4 in the execution_table.
If you insert 0.1234 into DECIMAL(5,2), what will happen to the stored value?
AIt stores 0.12 after rounding/truncation
BIt stores 0.1234 exactly
CIt stores 0.13 after rounding
DIt stores NULL
💡 Hint
DECIMAL(5,2) allows 2 decimal places; extra digits are rounded as per MySQL rules.
Concept Snapshot
DECIMAL stores exact decimal numbers with fixed precision.
FLOAT and DOUBLE store approximate values using binary floating-point.
Use DECIMAL for money or exact values.
FLOAT is faster but can have rounding errors.
Define DECIMAL as DECIMAL(M,D) where M=total digits, D=digits after decimal.
Full Transcript
This lesson shows how MySQL handles decimal and floating-point numbers. DECIMAL stores numbers exactly with fixed digits after the decimal point. FLOAT stores numbers approximately using binary floating-point, which can cause small rounding differences. We created a table with DECIMAL(5,2) and FLOAT columns, inserted values like 123.45 and 0.1, and selected them to see how they are stored. DECIMAL preserved the exact values, while FLOAT showed slight differences. This helps understand when to use DECIMAL for precise values like money, and FLOAT for approximate values where speed matters.