Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL values limit to 1 and 0

Tags:

mysql

Hello guys I'm just a newbie so sorry for my question :)

I already tried using char, tinyint, bool and CHECK constraint.

Here's my CHECK constraint:

CHECK (user_type>=0 AND user_type<=1)

But still I can insert values more than 1, all I want is just 1s and 0s. I'll use it as type of my users. Although I can validate this in front-end level, I still want do it in a database itself.

Thanks in advance :)

like image 883
Barry Avatar asked Dec 13 '25 07:12

Barry


2 Answers

use set or enum as the colum type. then define the values (eg: 1,0) :)

like image 134
Thomas Clayson Avatar answered Dec 15 '25 06:12

Thomas Clayson


The CHECK clause is parsed but ignored by all storage engines. (Source)

If you really want to do it you can create a table containing the values 0 and 1 and set up a foreign key constraint.

like image 41
Mark Byers Avatar answered Dec 15 '25 06:12

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!