How to set table in MySQL so that certain column INT
accept only positive values?
How to set table in MySQL so that certain column INT
accept only positive values?
The ideal is to make this type of control before the database, send the wrong data to return an error and having to fix it is the worst form of exception use I have ever seen. If it is known that only positive can validate this before in the application, simplify it and gain performance and robustness. In fact MySQL is a great database for storage, but lousy to handle this sort of thing, so it's best to make it very simple and handle everything in the application.
If you want you can document with a restriction in the column, but it will not be respected:
CHECK (coluna > 0)
Do not use unsigned
, it does not work too, and will not give error, only truncate the negative, which is not what you want.
Alright?
As the above friends mentioned, MYSQL can not limit acceptance of positive numbers only. If you use unsigned , what it will do is "convert" your negative number to positive, and this can compromise your database.
The best way to do this limitation is through the language you are using to make the connection to the bank. What language are you using, maybe I can help you.
Hugs.