简体   繁体   English

当枚举字段设置为无效值时,将MySQL默认设置为NULL

[英]Make MySQL default to a NULL when an enum field is set to an invalid value

Is there a way to make MySQL default to a NULL when an enum field is set to an invalid value? 当枚举字段设置为无效值时,是否可以将MySQL默认设置为NULL?

Also, is there a way I can specify a value in an HTML form such that, when saved to the DB, it stores as a NULL. 另外,有没有一种方法可以以HTML形式指定值,以便在保存到数据库时将其存储为NULL。 I don't want to do any checks like "if empty string, then save a NULL." 我不想做任何检查,例如“如果为空字符串,然后保存NULL”。 If it's possible, is it a good idea? 如果可能,这是个好主意吗? Any security concerns? 任何安全方面的顾虑?

My scenario: I have an HTML drop down menu that has an "null" option followed by a list of items that correspond to the DB's enum. 我的情况是:我有一个HTML下拉菜单,其中有一个“空”选项,后跟与数据库枚举相对应的项目列表。 When the null option is selected and saved, I want it to reflect as a NULL in the DB. 选择并保存null选项后,我希望它在DB中反映为NULL。 Right now, it's being converted to an empty string, as per: 现在,它被转换为空字符串,如下所示:

If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. 如果您在ENUM中插入无效值(即,在允许值列表中不存在的字符串),则会插入空字符串,作为特殊错误值。 This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 0. 通过将该字符串的数值设为0,可以将该字符串与“常规”空字符串区分开。

http://dev.mysql.com/doc/refman/5.5/en/enum.html http://dev.mysql.com/doc/refman/5.5/en/enum.html

" If an ENUM column is declared to permit NULL, the NULL value is a valid value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values. " 如果将ENUM列声明为允许NULL,则NULL值是该列的有效值,并且默认值为NULL。如果将ENUM列声明为NOT NULL,则其默认值是允许列表的第一个元素价值观。

On this basis, if you don't set a value for the field with your INSERT statement and you have declared the ENUM field to accept NULL, it will default to NULL. 在此基础上,如果未使用INSERT语句为该字段设置值,并且已声明ENUM字段接受NULL,则它将默认为NULL。

[EDIT] [编辑]

To address your comment, if you have this table definition 如果您有此表定义,请发表您的评论

CREATE TABLE IF NOT EXISTS `tbl_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` enum('New','Done') DEFAULT NULL,
  PRIMARY KEY (`id`)
)

And your web application is attempting to UPDATE and existing record using something like this 而且您的Web应用程序正尝试使用类似这样的东西更新和现有记录

UPDATE `test`.`tbl_test` SET `type` = NULL WHERE `tbl_test`.`id` =1

It will set the value to NULL. 它将值设置为NULL。

You can reduce the risk of invalid enum data appearing in your HMTL drop downs by querying the table for the possible ENUM values to dipaly in the drop down. 您可以通过查询表中可能包含的ENUM值以降低可能的ENUM值,从而降低在HMTL下拉列表中出现无效枚举数据的风险。 Here's a Php example I found 这是我发现的一个PHP例子

SHOW COLUMNS FROM `tbl_test` WHERE Field = 'type'

[EDIT] [编辑]

To answer your additional comment, if you want the user to be able to select a NULL value and also allow an empty string as a valid value you need to sepecify your drop down like this 要回答您的其他评论,如果您希望用户能够选择NULL值并且还允许将空字符串作为有效值,则需要像这样分隔下拉列表

<select>
  <option value="-1">-- not selected ---</option>
  <option value=""></option>
  <option value="Book">Book</option>
  <option value="Music">Music</option>
</select>

By using the <option value= ...> attribute you can control the value POSTed to your WebApp and can then process accordingly. 通过使用<option value= ...>属性,您可以控制POST到WebApp的值,然后可以进行相应的处理。 In this example a value of -1 would be translated to NULL by your WebApp 在此示例中,WebApp会将值-1转换为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM