MySQL Enum's always contain '' (empty string) in possibilities - mysql

I'm trying to create a simple 'yes'/'maybe'/'no' Enum in MySQL with PhpMyAdmin
I set NULL to No, and 'maybe' as the default value
I am expecting an error when executing something like "SET EnumCol=''", because '' (an empty string) should not be a valid value.
But the query gets executed and the value gets set to '' - which means I'm forced to double check for this unwanted and illegal value whenever I read from the database!
Is this a bug in MySQL or PhpMyAdmin?
Does anyone know a way of disabling this behavior?
Thanks.

Empty string is error indicator of invalid values in ENUM. From mysql ENUM type manual:
If you insert an invalid value into an ENUM (that is, a string not present in the list of allowed 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 numerical value 0. More about this later.
To disable this behaviour:
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
To enable strict mode see Server SQL Modes.

ENUM's are a pain in the butt. unless you also need to set the value by a number, i would stay away from them.
instead, use a varchar column with a foreign key to a lookup table to restrict the values. that will make it impossible to insert a bad value.

Related

Mysql enum field problems

in a mysql table I have a field of type Enum ('S', 'N') with default value = S. Now I have checked this table and I find many records that have no value in this field. How is it possible? I wanted to force the values of this field to be only S and N. Where did I go wrong?
Mysql version 5.0.92
my field definition:
`conteggia` enum('S','N') NOT NULL default 'S'
From the Empty or NULL Enumeration Values documentation:
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. ...
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
So you must not have strict mode enabled, and due to some error in your code it assigned invalid values to the column.

How restrict mysql tiny-int to accept just integer

Im trying to get eeror about invalid data for a query but it seems mysql accept strings as tiney in queries and set 0 instead of returning any error.
So how can I restrict it to just accept integers.
Thanks in advance.
This is SQL Mode-dependent.
In strict mode you must receive either 'Data truncated' (inserted value starts from numeric chars) or 'Incorrect integer value' (first symbol is not numeric) error message, and none data must be inserted.
If strict mode is disabled then warning is generated instead of error, and the value is converted to numeric (all symbols starting from first non-numeric one are truncated, then the value is converted to number, if it is empty string then zero is assigned).
Data type checking (and convertion if needed) is performed firstly, even before BEFORE INSERT trigger.
DEMO

Entering default value of string in database

I am trying to enter a default value of a string row in my database, using ASP.NET Visual Studio. I am simply trying to have "NotSet" as default but I get this error when trying to update the database:
The name "NotSet" is not permitted in this context. Valid expressions
are constants, constant expressions, and (in some contexts) variables.
Column names are not permitted.
I still have trouble understanding what kind of values are permitted, though. The datatype is "nchar(10)" and nulls are allowed. There's nothing else to it.
Make sure NotSet is in quotes in your sql statement
'NotSet'

Is a non-nullable, MySQL enum column guaranteed to only contain those values?

If I have a column in a MySQL database that is:
Non-nullable
Is an ENUM with 5 possible values
Has a default value of 1 of those ENUM's
Can it be guaranteed that there can never be a value in that column apart from one of those ENUM's?
Not by default - there is one more value your column can have in addition to the values of the enum, an empty string in the column.
You have to enable strict SQL mode in MySQL to have the guarantee that only the enum values (or NULL if the column is nullable ) can occur.
Taken from here
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. See Section 11.4.4, “ Index Values for Enumeration
Literals ” for details about the numeric indexes for the enumeration
values.
If strict SQL mode is enabled, attempts to insert invalid ENUM values
result in an error.
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
Source

MYSQL - Character string length 20, field accepts varchar(15), no error generated?

I'm building a PHP/HTML front end to a MySQL database.
The table I'm attempting to work with defined with a column that is varchar(15). I can run (without error) an insert statement with a character string that is 20 characters long. The resulting record's column is truncated to 15 characters, but no error is generated.
How do I get this to generate an error?
I know that the interface can do the error checking, but I want to know how to get the database to reject the data as well.
MySQL's fairly forgiving and will try to gracefully accept anything you pass it as best you can, silently converting/truncating/nulling if need be.
Since you don't want that, you need to enable the various "strict" mode options: http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html