MySQL无法插入DECIMAL值:ER_WARN_DATA_OUT_OF_RANGE错误求助
Alright, let's figure out why you're hitting that "Out of range value" error and fix it up.
Your DECIMAL(4,4) column definition is the issue here. Let's break down how the DECIMAL(M,D) type works in MySQL:
M= total number of digits (integer + fractional parts combined, not counting the decimal point)D= number of digits reserved exclusively for the fractional part
When you set DECIMAL(4,4), you're allocating all 4 available digits to the fractional portion. That means the integer part can only be 0—valid values are between -0.9999 and 0.9999. All your insert values (like 5.5, 8.2, or 10) have integer parts greater than 0, so they're way outside this range, triggering the error.
You need to tweak the DECIMAL parameters to match your actual data range. Looking at your sample inserts:
- The largest integer value is
10(2 digits) - Most values have 1 fractional digit (e.g.,
5.5,9.1)
A perfect fit here would be DECIMAL(3,1):
- 3 total digits (2 for the integer part, 1 for the fractional)
- Supports values from
-99.9to99.9, which covers all your sample data (including10, which will be stored as10.0).
Run this ALTER statement to update the column:
ALTER TABLE relationship MODIFY COLUMN student_grade DECIMAL(3, 1);
If you ever need more fractional precision (e.g., two decimal places like 5.50), switch to DECIMAL(4,2) instead—it allows integers up to 99 with two decimal digits.
After updating the column, your existing INSERT statements will run without errors. For example, 10 will be stored as 10.0 in the DECIMAL(3,1) column, which is fully valid.
内容的提问来源于stack exchange,提问作者Dronius

