Html 对于是/否复选框值,我应该使用哪个 MySQL 字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3675640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What MySQL field should I use for a Yes/No Checkbox value?
提问by John
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?
我正在使用下面的 MySQL 表。我想添加一个名为的字段'subcheck'
,该字段将是由 HTML 表单输入确定的是/否值TYPE = CHECKBOX
。我应该给这个新字段什么“类型”?
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`)
)
回答by alexn
回答by Ross
a boolean - 1 for yes, 0 for no.
布尔值 - 1 表示是,0 表示否。
(value of the checkbox will need to be 1 or 0 of course).
(复选框的值当然需要为 1 或 0)。
Much more portable than yes/no imo. efficient too
比是/否 imo 更便携。也有效率
回答by CharlesLeaf
I personally prefer a enum or set data type in MySQL for this. It keeps the data legible.
为此,我个人更喜欢 MySQL 中的 enum 或 set 数据类型。它使数据保持清晰。
回答by BalusC
I would use numeric(1) not null default 0
wherein 0
indicates false
and 1
indicates true
.
我会使用numeric(1) not null default 0
其中0
指示false
和1
指示true
。
回答by jwueller
I would recommend a TINYINT(1)
for this purpose. It stores either 1
or 0
, to indicate yes or no. It takes very little space and is better supported across different SQL-engines than regular bool-types.
我会TINYINT(1)
为此目的推荐一个。它存储1
或0
,以指示是或否。它占用很少的空间,并且比常规的 bool 类型在不同的 SQL 引擎中得到更好的支持。