distal-attribute
distal-attribute
distal-attribute
distal-attribute

How to insert integer as default value?

User, date Message
Written by darkangelMoney, Euro
5 years ago
Category: General
14 posts since Wed, 08 Jul 09
Hi,

I can't see an option to insert an integer as a default value in the table editor, only Text, NULL, CURRENT_TIMESTAMP, ON UPDATE CURRENT_TIMESTAMP, and AUTO_INCREMENT.

?

_da.
Written by ansgar
5 years ago
4961 posts since Fri, 07 Apr 06
Yes, you have to put numbers with the text option.

The reason for why numbers get treated as text here is that a SHOW CREATE TABLE also returns numbers as quoted strings. No chance to detect numeric values as such. However, a cosmetic problem, as the server will deny your ALTER statement if it contains an invalid default value.
Written by darkangelMoney, Euro
5 years ago
14 posts since Wed, 08 Jul 09

anse wrote: Yes, you have to put numbers with the text option.

The reason for why numbers get treated as text here is that a SHOW CREATE TABLE also returns numbers as quoted strings. No chance to detect numeric values as such. However, a cosmetic problem, as the server will deny your ALTER statement if it contains an invalid default value.


You know the data type of the column is numeric, so you could use that information to generate an unquoted value.

Then change "Text:" to "Value:" or whatever where you input the default value.

Just a thought.

_da.
Written by ansgar
5 years ago
4961 posts since Fri, 07 Apr 06
Unquoting numbers in the ALTER or CREATE code wouldn't make an effective difference, so that's why this issue is rather cosmetic. But changing the label to "Custom value" or whatever is a good idea, would make it clear that there can also be numbers.
Written by darkangelMoney, Euro
5 years ago
14 posts since Wed, 08 Jul 09
OK.
Written by darkangelMoney, Euro
5 years ago
14 posts since Wed, 08 Jul 09
Anse,

The issue is when you don't wish to specify a default value (you want to use the implicit default value for the column data type). Then you get CREATE code like this:

CREATE TABLE `my_table` (
`Column 1` INT(10) NOT NULL DEFAULT ''
)

... which gives you an error. You need to be able to specify a default value of "None" (which should probably be positioned before all other options).

Also, if an existing table has fields without explicit defaults, the CREATE code incorrectly shows a default value of '' (empty string) for integer types (it shouldn't show any default value – for other data types as well). On more than one occasion I've sent CREATE code to a friend and he's been like "WTF?".

Thanks,

_da (Glen).
Written by darkangelMoney, Euro
5 years ago
14 posts since Wed, 08 Jul 09
Just noticed issue #1261. :)
Written by ansgar
5 years ago
4961 posts since Fri, 07 Apr 06
Thanks!
 

Please login to leave a reply, or register at first.