0

I tried creating a field as a TINYINT(1), NOT NULL and a DEFAULT value of -1 to indicate 'unknown', but I got this error from my client:

Error altering MyTable: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

I also tried making the length 2 and got the same message

Guessing TINYINT isn't the right data type for this kind of designation, but what is?

EDIT: I got it working after reading Ed Cottrell's comment. I think there was an extra character somewhere, here was the statement that my client (using Querious for Mac) generated:

ALTER TABLE `DBName`.`MyTable`
CHANGE COLUMN `MyColumn` `MyColumn` TINYINT(1) NOT NULL DEFAULT -1  COMMENT ''
AFTER `MyOtherColumn`;

Noticed that COMMENT there and made sure everything was clean.

Other comments and answers were appreciated; I have decided to let NULL mean unknown in this case

4

1 回答 1

3

我认为您应该将其存储为一点(如果您关心存储大小),并NULL表示“未知”。

field bit(1) default NULL,

not null将一个字段声明为 is然后具有一个本质上意味着 . 的特殊值似乎很奇怪NULL

编辑:

以下语法在SQL Fiddle上“有效” :

create table t (
  val int,
  flag tinyint(1) default -1
 );

“works”用引号引起来,因为默认值打印为“1”而不是“-1”——毕竟,这(1)意味着只打印一个数字。

也许在某些早期版本的 MySQL 中,当它看到-1无法正确显示时会产生错误。(老实说,这让我感到惊讶。)

于 2013-11-07T22:52:42.410 回答