mysql Tinyint作为无符号

时间:2012-10-10 05:14:24

标签: mysql

我试着写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

3 个答案:

答案 0 :(得分:16)

UNSIGNED是一个可以添加到多种类型的属性。来自documentation

<强> DATA_TYPE

    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

答案 1 :(得分:6)

使用关键字unsigned标记无符号。所以,在为一个例子制作一张桌子时:

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

请参阅Create Table以获取更多信息, 或Alter table

答案 2 :(得分:1)

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

UPDATE table SET tiny_field = tiny_field + 128;

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