I am using java and MySQL. I am trying to insert a value into a table.
When I insert, I get this exception:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of
range value for column 'idPxxx' at row 1
Only 5% of inserts produces this exception, others work. What does the error mean and how do I prevent it?
When MySQL stores a value in a numeric column that is outside the permissible range of the column data type, the result depends on the SQL mode in effect at the time: If strict SQL mode is enabled, MySQL rejects the out-of-range value with an error, and the insert fails, in accordance with the SQL standard.
The TRUNCATE TABLE statement is used to remove all records from a table in MySQL. It performs the same function as a DELETE statement without a WHERE clause. Warning: If you truncate a table, the TRUNCATE TABLE statement can not be rolled back.
In databases and computer networking data truncation occurs when data or a data stream (such as a file) is stored in a location too short to hold its entire length.
It means that the data you are storing in idPxxx
doesn't fit. For example a string might be too long, or a number too large.
What is the datatype of idPxxx
? And what are you trying to store in it?
How to produce this Exception on the MySQL console:
mysql> create table penguin (mydecimal decimal(9,8));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into penguin values (1234.1234);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+----------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------+
| Warning | 1264 | Out of range value for column 'mydecimal' at row 1 |
+---------+------+----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from penguin;
+------------+
| mydecimal |
+------------+
| 9.99999999 |
+------------+
1 row in set (0.00 sec)
You tried to cram 1234 into a column that could take maximum 9.9 repeated. Notice the row is still inserted.
You can get the MySQL console to prevent this conversion using strict mode:
mysql> set sql_mode=STRICT_ALL_TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into penguin values (5678.5678);
ERROR 1264 (22003): Out of range value for column 'mydecimal' at row 1
The insert command failed in attempting add a second row to penguin:
mysql> select * from penguin;
+------------+
| mydecimal |
+------------+
| 9.99999999 |
+------------+
1 row in set (0.00 sec)
Solutions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With