MySQL 单选按钮的mysql数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8305649/
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 data type for radio buttons?
提问by Leahcim
I'm making a form with radio buttons (see http://jsfiddle.net/mjmitche/3c6Mc/) and users are submitting data mostly through radio buttons. What "type" will the values for the radio buttons have to be/should they be in the mysql table?
我正在制作一个带有单选按钮的表单(参见http://jsfiddle.net/mjmitche/3c6Mc/),用户主要通过单选按钮提交数据。单选按钮的值必须/应该在 mysql 表中是什么“类型”?
The fields advocacy, drafting, interview etc will all be either "pass" or "fail" on the webform, so what do I do in the database?
网络表单上的领域宣传、起草、面试等都将是“通过”或“失败”,那么我在数据库中做什么?
CREATE TABLE `shoutbox`(
`id` int(5) NOT NULL auto_increment,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP,
`instructor` varchar(25) NOT NULL ,
`secretmessage` varchar(255) NOT NULL default '',
`advocacy` NOT NULL,
`drafting` NOT NULL,
`interview`
`letter`
`solicitor`
`barrister`
PRIMARY KEY (`id`)
);
Boolean
布尔值
If I use a Boolean, do I set the values for Pass and Fail to 1 and 0?
如果我使用布尔值,是否将 Pass 和 Fail 的值设置为 1 和 0?
<tr>
<td><label>Interview</label></td>
<td><input type="radio" name="interview" value="1" /> Pass</td>
<td><input type="radio" name="interview" value="0" /> Fail</td>
</tr>
with this mysql?
用这个mysql?
CREATE TABLE `shoutbox`(
`interview` BOOLEAN NOT NULL,
TinyINT
TinyINT
If I use a TinyInt, do I also set the value of Pass and Fail to 1 and 0?
如果我使用 TinyInt,是否还要将 Pass 和 Fail 的值设置为 1 和 0?
<tr>
<td><label>Interview</label></td>
<td><input type="radio" name="interview" value="1" /> Pass</td>
<td><input type="radio" name="interview" value="0" /> Fail</td>
</tr>
CREATE TABLE `shoutbox`(
`interview` TINYINT NOT NULL,
ENUM
枚举
If I use enum, do I set the value to "pass" and "fail" and then use the same names in the database with enum?
如果我使用枚举,是否将值设置为“通过”和“失败”,然后在数据库中使用与枚举相同的名称?
<td><label>Interview</label></td>
<td><input type="radio" name="interview" value="pass" /> Pass</td>
<td><input type="radio" name="interview" value="fail" /> Fail</td>
</tr>
Mysql
mysql
CREATE TABLE `shoutbox`(
`interview` enum('pass', 'fail') NOT NULL,
采纳答案by Amado Martinez
ENUM may be behave unexpectedly if inserting integer values.
如果插入整数值,ENUM 可能会出现意外行为。
Example:
例子:
CREATE TABLE `shoutbox`(
`interview` enum('pass', 'fail') NOT NULL);
INSERT INTO shoutbox (0); // this would be equivalent to an empty ENUM value ('')
INSERT INTO shoutbox (1); // represents 'pass', 2 represents 'fail'
INSERT INTO shoutbox ('pass'); // works as expected, same for 'fail'.
If you used ENUM, it would be best to validate the radio input value so that no empty or invalid strings are passed.
如果您使用 ENUM,最好验证无线电输入值,以免传递空字符串或无效字符串。
$interview = in_array($_POST['interview'], array('pass', 'fail')) ? $_POST['interview'] : 'fail';
I recommend using an BOOLEAN or TINYINT and validating the radio input value by using intval();
我建议使用 BOOLEAN 或 TINYINT 并使用 intval() 验证无线电输入值;
CREATE TABLE `shoutbox`(
`interview` tinyint(1) NOT NULL);
php:
php:
$interview = intval($_POST['interview']); // sets interview to 0 or 1
// "insert into shoutbox($interview);"
回答by Eric J.
You should think of this in terms of what the radio buttons represent in the real world.
您应该根据单选按钮在现实世界中代表的内容来考虑这一点。
The answer is probably that they represent a short list of mutually exclusive things. Generally those are represented in a business object with an enum, which can be saved most space-efficiently as a tinyint in the database.
答案可能是它们代表了一个简短的互斥事物列表。通常,这些在带有枚举的业务对象中表示,作为数据库中的 tinyint 可以最节省空间。
A [tinyint][1] can represent values from -128 to 127, so works for up to 256 different values (assuming your language can map enum values to negative numbers) or up to 128 different values if your enums are restricted to non-negative numbers. If you have more different values (which would make for a hard-to-use UI), you could step up to a smallint.
[tinyint][1] 可以表示从 -128 到 127 的值,因此最多可用于 256 个不同的值(假设您的语言可以将枚举值映射到负数)或最多 128 个不同的值(如果您的枚举仅限于非)负数。如果您有更多不同的值(这会使 UI 难以使用),您可以升级到 smallint。
For things that only have two values, you still won't get more efficient than using a tinyint (short of mapping multiple questions into different bit positions of a numeric field, which I strongly discourage under all but the most extreme conditions). MySQL does support a [bit
data type][2] that supports bit fields, but I doubt that bit(1) would still not use less storage than tinyint (it should use the same amount, but I could not find a specific reference).
对于只有两个值的事物,您仍然不会比使用 tinyint 更有效率(除了将多个问题映射到数字字段的不同位位置之外,我强烈建议不要在最极端的情况下这样做)。MySQL 确实支持bit
支持位字段的 [数据类型][2],但我怀疑 bit(1) 仍然不会使用比 tinyint 更少的存储(它应该使用相同的数量,但我找不到特定的参考)。
EDIT:
编辑:
Regarding your edit asking about how to use the MySQL enum datatype, I actually do not recommend it's use if you have a business logic layer. The reason for that is that you need to maintain the enum definition in two places (the business object and the DDL for MySQL). So, I would go with your Tinyint example. I would also have the business layer generate the values into your UI rather than hard coding them.
关于您的编辑询问如何使用 MySQL 枚举数据类型,如果您有业务逻辑层,我实际上不建议使用它。这样做的原因是您需要在两个地方(业务对象和 MySQL 的 DDL)维护枚举定义。所以,我会用你的 Tinyint 例子。我还会让业务层将值生成到您的 UI 中,而不是对其进行硬编码。
Using the Boolean data type is clearer if you genuinely have a Boolean condition. However, right now you have a list of values that will "only ever have two values". Experience shows that truth will change during the lifetime of your program more often than you might expect (maybe "Pass" and "Fail" gets additional values like "Incomplete Interview" or whatever).
如果您确实有布尔条件,则使用布尔数据类型会更清晰。但是,现在您有一个“只有两个值”的值列表。经验表明,在你的程序生命周期中,真相会比你预期的更频繁地改变(也许“通过”和“失败”会得到额外的值,比如“不完整的面试”或其他什么)。
回答by Kevin
In general, enum
type would back a radio set best, but since your options are pass / fail, you could use bool
.
一般来说,enum
type 最能支持收音机,但由于您的选项是通过/失败,您可以使用bool
.