31

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

3 Answers 3

25

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
15

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.

1
  • In case you need to add a new tinyint column : ALTER TABLE tablename ADD COLUMN colname TINYINT unsigned; Jan 5, 2022 at 22:58
2

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.

1
  • 2
    You should have a where tiny_field<0 clause on this Dec 19, 2018 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.