简体   繁体   中英

using converting string to tinyint (bool) with mysql

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM