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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:26:53  来源:igfitidea点击:

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

mysqlhtml

提问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

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

您可以使用TINYINT(1)(BOOL / BOOLEAN只是一个别名TINYINT(1))。

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

另一种选择是存储Y/NCHAR(1).

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

我会推荐TINYINT(1)它,因为它会给你最好的便携性选择。

回答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 0wherein 0indicates falseand 1indicates true.

我会使用numeric(1) not null default 0其中0指示false1指示true

回答by jwueller

I would recommend a TINYINT(1)for this purpose. It stores either 1or 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)为此目的推荐一个。它存储10,以指示是或否。它占用很少的空间,并且比常规的 bool 类型在不同的 SQL 引擎中得到更好的支持。