Is there a way, using a TRIGGER or preferably some MySql function I’m not aware of, to automatically assign the value of a TINYINT column to ‘1’ if ANY value is passed into it?
For example if I have columnA (TINYINT) and I INSERT “doggybones” into that column:
INSERT INTO mytable (columnA) VALUES ("doggybones")
… then mysql would automatically assign a value of “1” to columnA instead of kicking up an error about trying to stick a string into a TINYINT column?
It should also work so that if I insert “” – a blank value but the INSERT statement will still list it, like so:
INSERT INTO mytable (columnA) VALUES ("")
… then the column will receive a value of 0.
Note: I know I could filter my values to just INSERT 0’s and 1’s but I’m wondering if there is a way to do this on the mysql side.
Unfortunately, SQL (all flavours) enforce data types strictly.
This happens before any trigger code runs.
You’ll have to test for “” (empty string)