mysql Tinyint as unsigned

前端 未结 3 789
臣服心动
臣服心动 2021-02-12 11:05

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 (b

相关标签:
3条回答
  • 2021-02-12 11:18

    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.

    0 讨论(0)
  • 2021-02-12 11:30

    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.

    0 讨论(0)
  • 2021-02-12 11:33

    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
    
    0 讨论(0)
提交回复
热议问题