Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:51:34+00:00 2026-06-04T00:51:34+00:00

Is there a way to make MySQL default to a NULL when an enum

  • 0

Is there a way to make MySQL default to a NULL when an enum field is set to an invalid value?

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. I don’t want to do any checks like “if empty string, then save a 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. When the null option is selected and saved, I want it to reflect as a NULL in the DB. 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. This string can be distinguished
from a “normal” empty string by the fact that this string has the
numeric value 0.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:51:35+00:00Added an answer on June 4, 2026 at 12:51 am

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

    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.

    [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

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

    It will set the value to 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. Here’s a Php example I found

    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

    <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. In this example a value of -1 would be translated to NULL by your WebApp

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in MySQL table to assign a default when a value
Does anybody know if there is a way to make autocompletion work in MySQL
I am wondering if there is a better way to make some mysql tables
Is there any way to connect external data base like mysql can be connected
Is there a way to make mysql workbench dump automaticly the data? I know
Is there a way to make the banner images that are displayed in the
Is there a way to make MatrixForm display a row vector horizontally on the
Is there a way to make a TSQL variable constant?
Is there a way to make a Radio Button enabled/disabled (not checked/unchecked) via CSS?
Is there a way to make a popup window maximised as soon as it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.