Knowledgebase

Portal Home > Knowledgebase > Articles Database > mysql data never go under


mysql data never go under




Posted by ti_nhatrang, 08-24-2011, 03:43 PM
Hi all, I want to learn how to setup my mysql column/field to a solid number that the field can never go under during an update. For example, I want to set a field that can only be updated 1 and above. I don't want it to update .009 or whatever. Please help me how to do this, thank you.

Posted by Website themes, 08-24-2011, 04:32 PM
Just set the data type to integer.

Posted by 8088, 08-24-2011, 04:48 PM
2, -1 and 0 are integers, too.

Posted by lynxus, 08-24-2011, 05:00 PM
My understanding is that its NOT possible. The only real way to do this is by adding logic to your insert code.. ie: $val = "-1"; if ($val >= "0") { //insert code }else{ //dont }

Posted by ti_nhatrang, 08-24-2011, 05:11 PM
well, i was trying to keep it from going under '0.03' - i was trying to bypass the logic to the insert/update code...

Posted by Website themes, 08-24-2011, 06:40 PM
Well you can set it to an unsigned integer in which case it will always be positive. -1 will still be stored but as +1.

Posted by ti_nhatrang, 08-25-2011, 03:41 AM
hmm, i was trying to avoid that either. do not want it to go -200 and it becomes 200...

Posted by Website themes, 08-25-2011, 04:11 AM
Ok I was wrong above when I said negative numbers will be turned into positive ones in an unsigned integer column. Actually negative numbers are considered out of range so they turn into 0. So if you try to insert -1 it'll actually be saved as 0. If you want it generate an error instead of doing that you can set strict mode: SET sql_mode = 'STRICT_ALL_TABLES'; http://dev.mysql.com/doc/refman/5.0/...alid-data.html Now if you try to insert a negative integer it will error out. But if you insert a floating point number like 0.34 it will convert it to 0 So we are back to square one. Looks like lynxus was right. You have to do this in your script.

Posted by ti_nhatrang, 08-25-2011, 06:25 AM
Yeah, i need that column to always stay at 0.03 or higher...

Posted by Acidx, 08-26-2011, 11:02 PM
you could use floats right?



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Error 500 - cPanel (Views: 565)
Nameservers setup (Views: 565)


Language:

Contact us