Description:
When you perform an INSERT statement and one (or more) columns are larger that the column size, the row is not inserted.
How to repeat:
Look at the MYSQL code:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17 to server version: 5.1.11-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> connect test
Connection id: 18
Current database: test
mysql> create TABLE TEST3 (A VARCHAR(4));
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO TEST3 VALUES('prova');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'A' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
mysql> select * from test3;
+------+
| A |
+------+
| prov |
+------+
1 row in set (0.03 sec)
Although the string lenght is larger than the maximum, the row is inserted (and truncated). Instead, using this java code:
import java.sql.*;
public class l1 {
public static void main (String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://10.39.64.94/test","root","root");
Statement st = conn.createStatement();
try {
st.executeUpdate("INSERT INTO TEST3 VALUES('prova');");
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getSQLState());
System.out.println(e.getErrorCode());
System.out.println(e.getCause());
System.out.println(e.getMessage());
}
st.close();
conn.close();
}
}
I got an error:
------------------------------------------------------------------------------------------------
C:\>java -cp javalib\mysql-connector-java-5.0.3-bin.jar;. l1
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'A' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2973)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2998)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1071)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1005)
at l1.main(l1.java:10)
01004
0
null
Data truncation: Data too long for column 'A' at row 1
C:\>
------------------------------------------------------------------------------------------------
C:\>java -cp javalib\mysql-connector-java-5.0-nightly-20061123-bin.jar;. l1
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'A' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2869)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1574)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3208)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1316)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1235)
at l1.main(l1.java:10)
01004
0
null
Data truncation: Data too long for column 'A' at row 1
------------------------------------------------------------------------------------------------
And no data is inserted in the table. Altering the column size (from varchar(4) to varchar(5) the problem disappears because there is no more data truncation).