Error
Error Code:
1427
MySQL Error 1427: Invalid Numeric Precision/Scale
Description
This error indicates that the total number of digits (M) specified for a FLOAT, DOUBLE, or DECIMAL column is less than the number of digits allowed after the decimal point (D). It typically occurs during table creation or alteration when defining a numeric column with an invalid precision and scale combination, violating the rule M >= D.
Error Message
For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '%s').
Known Causes
3 known causesIncorrect Precision/Scale Definition
Specifying a value for M (total digits) that is smaller than D (decimal digits) when defining a FLOAT, DOUBLE, or DECIMAL column in a DDL statement.
Typographical Error in DDL
A simple mistake in typing the M and D values in your CREATE TABLE or ALTER TABLE statement, leading to an invalid M < D condition.
Misunderstanding Parameter Usage
Not fully grasping that M (total precision) must always be greater than or equal to D (scale) for FLOAT, DOUBLE, and DECIMAL data types.
Solutions
3 solutions available1. Adjusting Numeric Precision and Scale easy
Correct the M and D values in the column definition to satisfy M >= D.
1
Identify the table and column causing the error. The error message usually indicates the column name.
2
Determine the intended precision (M) and scale (D) for the numeric column. Ensure that M is greater than or equal to D. For example, if you need to store numbers with up to 5 digits before the decimal and 2 digits after, you would use `DECIMAL(7, 2)`. If you need to store numbers with up to 3 digits after the decimal, `DECIMAL(3, 3)` is valid, but `DECIMAL(2, 3)` is not.
3
Modify the table schema to correct the precision and scale. Replace `your_table_name`, `your_column_name`, `M`, and `D` with your actual values. Ensure `M` >= `D`.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name DECIMAL(M, D);
4
If the column is a `FLOAT` or `DOUBLE`, you generally do not specify precision and scale (M, D) directly in the same way as `DECIMAL`. If you encounter this error with `FLOAT` or `DOUBLE`, it likely means you've incorrectly tried to specify M and D. Remove them or use the correct syntax if applicable (though typically not recommended for FLOAT/DOUBLE in this context). For `FLOAT(M,D)` and `DOUBLE(M,D)`, M is the total number of digits, and D is the number of digits after the decimal. The error message implies you've used an invalid combination.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name FLOAT;
-- or if you intended a specific precision, research MySQL FLOAT/DOUBLE documentation for correct usage, but often it's just FLOAT or DOUBLE.
2. Using Default or Simplified Numeric Types easy
Revert to simpler numeric types or remove invalid precision/scale specifications.
1
If the precise precision and scale are not critical, consider using a simpler numeric type.
2
For `DECIMAL` columns where the `M >= D` rule is violated, simply remove the invalid scale or adjust it to be valid. For example, if `DECIMAL(2, 3)` was used, change it to `DECIMAL(3, 3)` or `DECIMAL(3, 0)` if appropriate.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name DECIMAL(3, 3);
3
For `FLOAT` or `DOUBLE`, if you have specified `(M,D)` and are encountering this error, it's often best to remove the `(M,D)` entirely and let MySQL use its default precision. This is a common quick fix if the exact precision isn't a strict requirement.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name FLOAT;
3. Reviewing and Correcting Data Insertion/Update Statements medium
Ensure values being inserted or updated conform to the column's defined precision and scale.
1
Examine your `INSERT` and `UPDATE` statements that are attempting to modify the problematic column. The error might not be in the table definition itself, but in the data being provided.
2
If the column is `DECIMAL(M, D)`, ensure that the numbers you are trying to insert or update do not have more than `D` digits after the decimal point, and that the total number of digits (including those before and after the decimal) does not exceed `M`. For example, if the column is `DECIMAL(5, 2)`, you cannot insert `1234.567` or `12345.67`.
3
Before executing your `INSERT` or `UPDATE` statement, round or truncate the numeric values to fit the column's definition. You can use SQL functions like `ROUND()` or `TRUNCATE()`.
UPDATE your_table_name SET your_column_name = ROUND(your_value, D) WHERE ...;
-- or
INSERT INTO your_table_name (your_column_name, ...) VALUES (ROUND(your_value, D), ...);
4
Alternatively, adjust your application logic to format the numbers correctly before sending them to the database.