Bluehost.com Web Hosting $6.95

Enum data type

This is a discussion on Enum data type within the MySQL Database forums, part of the Database Forums category; So I setup a field as an enum data type: field1 enum('a','b','c') So when I tried to ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-06-2007
booner
 
Posts: n/a
Default Enum data type

So I setup a field as an enum data type:

field1 enum('a','b','c')

So when I tried to insert a value 'd' I was surprised when I did not receive
an error - and instead the value was inserted as an empty string.

How can I enforce that only the enum values are entered - i.e. receive an
error when attempting to enter a non enum type value?


Reply With Quote
  #2 (permalink)  
Old 03-06-2007
Martijn Tonies
 
Posts: n/a
Default Re: Enum data type


> So I setup a field as an enum data type:
>
> field1 enum('a','b','c')
>
> So when I tried to insert a value 'd' I was surprised when I did not

receive
> an error - and instead the value was inserted as an empty string.
>
> How can I enforce that only the enum values are entered - i.e. receive an
> error when attempting to enter a non enum type value?


ENUM doesn't enforce a check constraint.

It might be depending on some obscure setting though :-)

Alternatively, use triggers to force the value.


--
Martijn Tonies
Database Workbench - development tool for MySQL, and more!
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com


Reply With Quote
  #3 (permalink)  
Old 03-06-2007
Captain Paralytic
 
Posts: n/a
Default Re: Enum data type

On 6 Mar, 13:26, "booner" <bbo...@insightbb.com> wrote:
> So I setup a field as an enum data type:
>
> field1 enum('a','b','c')
>
> So when I tried to insert a value 'd' I was surprised when I did not receive
> an error - and instead the value was inserted as an empty string.
>
> How can I enforce that only the enum values are entered - i.e. receive an
> error when attempting to enter a non enum type value?


Maybe you had the field set to allow null values

Reply With Quote
  #4 (permalink)  
Old 03-06-2007
Captain Paralytic
 
Posts: n/a
Default Re: Enum data type

On 6 Mar, 13:26, "booner" <bbo...@insightbb.com> wrote:
> So I setup a field as an enum data type:
>
> field1 enum('a','b','c')
>
> So when I tried to insert a value 'd' I was surprised when I did not receive
> an error - and instead the value was inserted as an empty string.
>
> How can I enforce that only the enum values are entered - i.e. receive an
> error when attempting to enter a non enum type value?


Mid you, when I just tried it I got

#1265 - Data truncated for column 'field1' at row 1

Reply With Quote
  #5 (permalink)  
Old 03-06-2007
strawberry
 
Posts: n/a
Default Re: Enum data type

On Mar 6, 1:26 pm, "booner" <bbo...@insightbb.com> wrote:
> So I setup a field as an enum data type:
>
> field1 enum('a','b','c')
>
> So when I tried to insert a value 'd' I was surprised when I did not receive
> an error - and instead the value was inserted as an empty string.
>
> How can I enforce that only the enum values are entered - i.e. receive an
> error when attempting to enter a non enum type value?


FWIW, I enforce it in php at point of input - so a drop down list or
radio button in a form for instance.

Reply With Quote
  #6 (permalink)  
Old 03-06-2007
booner
 
Posts: n/a
Default Re: Enum data type

Dang - I'm at MySQL 4.1.10.

Triggers, constraints - 5.x features. Alright now I know what I need to do
:-)

Thx!

"Peter H. Coffin" <hellsop@ninehells.com> wrote in message
news:slrneur15g.7a4.hellsop@abyss.ninehells.com...
> On Tue, 6 Mar 2007 08:26:52 -0500, booner wrote:
>> So I setup a field as an enum data type:
>>
>> field1 enum('a','b','c')
>>
>> So when I tried to insert a value 'd' I was surprised when I did not
>> receive
>> an error - and instead the value was inserted as an empty string.
>>
>> How can I enforce that only the enum values are entered - i.e. receive an
>> error when attempting to enter a non enum type value?

>
> Constraint against a table that contains the acceptable values.
>
> --
> It is odd, but on the infrequent occasions when I have been called upon
> in a formal place to play the bongo drums, the introducer never seems to
> find it necessary to mention that I also do theoretical physics. --Feynman



Reply With Quote
  #7 (permalink)  
Old 03-08-2007
Norman Peelman
 
Posts: n/a
Default Re: Enum data type

booner wrote:
> So I setup a field as an enum data type:
>
> field1 enum('a','b','c')
>
> So when I tried to insert a value 'd' I was surprised when I did not receive
> an error - and instead the value was inserted as an empty string.
>
> How can I enforce that only the enum values are entered - i.e. receive an
> error when attempting to enter a non enum type value?
>
>
>

Take a look at:

http://dev.mysql.com/doc/refman/4.1/...aint-enum.html

and

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

....you should be able to specify the field with NOT NULL to force to to
default to the first enum entry.

Norm


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0