user34537
user34537

Reputation:

MySQL TINYINT as unsigned

I tried to write t=t|128 but got an out of range error. I suspect tinyint is signed.

However http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html says it may be unsigned (below). But it doesn't say how to make it unsigned. How do I?

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

Upvotes: 33

Views: 54123

Answers (3)

Devart
Devart

Reputation: 122032

Firstly, you should do something with negative values, otherwise ALTER TABLE...UNSIGNED will throw an error. For example you could increase all values -

UPDATE table SET tiny_field = tiny_field + 128;

Then use ALTER TABLE to change field type/option.

Upvotes: 2

Grumpy
Grumpy

Reputation: 1478

You mark the unsigned with the keyword unsigned. So, when making a table for an example:

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

See Create Table instead for more, or the Alter table.

Upvotes: 19

wallyk
wallyk

Reputation: 57794

UNSIGNED is an attribute which can be added to many types. From the 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

Upvotes: 26

Related Questions