Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a Boolean field to MySQL?

Tags:

php

mysql

boolean

It seems I should use tinyint(); but I don't know how to implement it?

The question is what is your recommendation if I need to have a boolean field in MySQL DB and modify it´s value with PHP

like image 441
Trufa Avatar asked Oct 14 '10 07:10

Trufa


People also ask

How do I add a boolean field in MySQL?

You can use tinyint(1) or bool or boolean. All are synonym. If you use bool or boolean datatype, then it nternally change into tinyint(1). In PHP, the value 0 represents false and 1 represents true.

How do you insert a boolean in SQL?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.

Can I use boolean in MySQL?

To deal with Boolean in MySQL, you can use BOOL or BOOLEAN or TINYINT(1). If you use BOOL or BOOLEAN, then MySQL internally converts it into TINYINT(1). In BOOL or BOOLEAN data type, if you use true literal then MySQL represents it as 1 and false literal as 0 like in PHP/ C/ C++ language.

How do you make a boolean column?

To create a column with 'false' as the default value, we can use the concept of “default” at the time of creation of the table. Note − 0 represents false and 1 represents true. Creating a table using “default” false.


1 Answers

Yep, TINYINT(1) is the way to go... you can also use BOOL or BOOLEAN which are synonyms (so it does not make a difference).

0 evaluates to false in PHP and 1 to true (actually, any other number than 0 evaluates to true, but 1 is normally used).

like image 122
Felix Kling Avatar answered Sep 25 '22 23:09

Felix Kling