28

我试图写 t=t|128 但出现超出范围的错误。我怀疑 tinyint 已签名。

但是http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html说它可能是未签名的(如下)。但它并没有说明如何使其未签名。我如何?

Type     Storage    Minimum Value        Maximum Value
         (Bytes)    (Signed/Unsigned)   (Signed/Unsigned)
TINYINT     1           -128                    127
                          0                     255
SMALLINT    2          -32768                   32767
                          0                     65535
MEDIUMINT   3          -8388608                8388607
                          0                   16777215
INT         4         -2147483648            2147483647
                          0                  4294967295
BIGINT      8      -9223372036854775808  9223372036854775807
                          0             18446744073709551615
4

3 回答 3

21

UNSIGNED是可以添加到多种类型的属性。从文档中:

数据类型

    BIT[(length)]
  | TINYINT[(length)] [UNSIGNED] [ZEROFILL]
  | SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
  | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
  | INT[(length)] [UNSIGNED] [ZEROFILL]
  | INTEGER[(length)] [UNSIGNED] [ZEROFILL]
  | BIGINT[(length)] [UNSIGNED] [ZEROFILL]
  | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
  | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
  | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
  | DATE
  | TIME
  | TIMESTAMP
  | DATETIME
  | YEAR
  | CHAR[(length)]
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | VARCHAR(length)
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | BINARY[(length)]
  | VARBINARY(length)
  | TINYBLOB
  | BLOB
  | MEDIUMBLOB
  | LONGBLOB
  | TINYTEXT [BINARY]
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | TEXT [BINARY]
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | MEDIUMTEXT [BINARY]
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | LONGTEXT [BINARY]
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | ENUM(value1,value2,value3,...)
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | SET(value1,value2,value3,...)
      [CHARACTER SET charset_name] [COLLATE collation_name]
  | spatial_type
于 2012-10-10T05:23:18.090 回答
14

你用关键字标记无符号unsigned。因此,在制作表格时:

CREATE TABLE `example_table` (
  `example_col` tinyint(3) unsigned NOT NULL
);

更多信息请参见创建表,或更改表

于 2012-10-10T05:25:07.813 回答
2

首先,你应该用负值做一些事情,否则ALTER TABLE...UNSIGNED会抛出错误。例如,您可以增加所有值 -

UPDATE table SET tiny_field = tiny_field + 128;

然后使用 ALTER TABLE 更改字段类型/选项。

于 2012-10-10T05:35:07.687 回答