Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What MySQL field should I use for a Yes/No Checkbox value?

Tags:

html

mysql

I am using the MySQL table below. I would like to add a field called 'subcheck' that will be a yes/no value determined by the HTML form input TYPE = CHECKBOX. What "type" should I give this new field?

Thanks in advance,

John

`submission` (
  `submissionid` int(11) unsigned NOT NULL auto_increment,
  `loginid` int(11) NOT NULL,
  `title` varchar(1000) NOT NULL,
  `slug` varchar(1000) NOT NULL,
  `url` varchar(1000) NOT NULL,
  `displayurl` varchar(1000) NOT NULL,
  `datesubmitted` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`submissionid`)
)
like image 619
John Avatar asked Sep 09 '10 10:09

John


People also ask

What is the data type for checkbox in MySQL?

MySQL does have BOOL and BOOLEAN data types, but they are synonymous with INT(1). So this is the type you would use with the possible values 0,1, or NULL. 1 would be true (checked). 0 would be false.

What is the datatype for boolean in MySQL?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

How do I add a boolean column in MySQL?

ALTER TABLE users ADD bio VARCHAR(100) NOT NULL; Adding a boolean column with a default value: ALTER TABLE users ADD active BOOLEAN DEFAULT TRUE; MySQL offers extensive documentation on supported datatypes in their documentation.

How do I add a boolean value to a database?

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'.


2 Answers

You can use a TINYINT(1) (BOOL/BOOLEAN is just an alias for TINYINT(1)).

Another option is to store Y/N in a CHAR(1).

I would recommend TINYINT(1) as it would give you best portability options.

like image 188
alexn Avatar answered Oct 13 '22 00:10

alexn


a boolean - 1 for yes, 0 for no.

(value of the checkbox will need to be 1 or 0 of course).

Much more portable than yes/no imo. efficient too

like image 40
Ross Avatar answered Oct 12 '22 23:10

Ross