Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL enums empty value

Tags:

mysql

I am trying to understand how enums could be used in mysql. If I insert anything to enum field that is out of the enum type -- mysql inserts empty string (with value 0).

  • fruit ENUM ('APPLE','BANANA','PEACH');
  • INSERT ... fruit='BANNANA'

Simple misspelling and MySQL inserts empty value, breaks database integrity and makes enums extremely useless.

CHECK constraints could help here but MySQL does not support them (pretty funny for "most popular" database in 2011)

The only way I see is to write the trigger to prevent empty string but it is too much work to write trigger for such a simple case.

Does there is a way to disable "empty string" MySQL behavior for enums?

Thanks

like image 824
user996142 Avatar asked Nov 17 '11 21:11

user996142


1 Answers

Take a look at sql modes

SET SQL_MODE='TRADITIONAL';
like image 135
Nicola Cossu Avatar answered Nov 16 '22 03:11

Nicola Cossu