Why does MySQL allow text to be inserted into an integer(INT) field?
For example, I have this:
INSERT INTO `estudiante` (`usuarioID`, `identidad`, `correo`, `institucion`)
VALUES ("32", 'xxx', '[email protected]', 'exterior')
the second field is identity which is integer because it doesn't give me an error when inserting a text
What am I doing wrong? it's supposed to be an int it shouldn't allow text, it converts it to zero
Here is the table layout:
CREATE TABLE `estudiante` (
`usuarioID` INT(11) NOT NULL,
`identidad` INT(20) NOT NULL,
`correo` VARCHAR(40) NOT NULL,
`institucion` VARCHAR(80) NOT NULL,
PRIMARY KEY (`usuarioID`)
)
The behavior you observe is described in the documentation here :
(Poor) translation:
Side note, this is one of several reasons I don't have much respect for MySQL as a database. If you care to read the entire page, it's amazing how MySQL, by default, is willing to twist/convert data in order to avoid giving you an error. What they call doing you a favor, I call data corruption.
In any case, applying the documented rule, this means that MySQL inserts and converts the following strings like this:
Demo
If, like me, you don't find this behavior correct, you can modify the way MySQL handles these situations. Again, as the documentation says :
(Poor) translation:
And, in fact, if you make sure that
sql_mode
your session includesSTRICT_TRANS_TABLES
, you will see that MySQL will throw an error.Demo
To see the value you have for
sql_mode
:Example of how to modify
sql_mode
your session:For more information about SQL modes you can follow the following link: Server SQL Modes .
In those cases MySQL does a conversion:
I mean:
Source: MySQL Reference Manual