308

What does "unsigned" mean in MySQL and when should I use it?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
HELP
  • 14,237
  • 22
  • 66
  • 100

2 Answers2

594

MySQL says:

All integer types can have an optional (nonstandard) attribute UNSIGNED. Unsigned type can be used to permit only nonnegative numbers in a column or when you need a larger upper numeric range for the column. For example, if an INT column is UNSIGNED, the size of the column's range is the same but its endpoints shift from -2147483648 and 2147483647 up to 0 and 4294967295.

When do I use it ?

Ask yourself this question: Will this field ever contain a negative value?
If the answer is no, then you want an UNSIGNED data type.

A common mistake is to use a primary key that is an auto-increment INT starting at zero, yet the type is SIGNED, in that case you’ll never touch any of the negative numbers and you are reducing the range of possible id's to half.

Pang
  • 9,564
  • 146
  • 81
  • 122
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

What does "unsigned" mean in MySQL:

In schema building (database design), the unsigned integer is an integer that can only hold a positive (non-negative) value.

In the context of mySQL, the unsigned method used to specify that column should only hold a positive value.

when should I use it?

It's good practice in schema building to assign the columns that hold foreign key (as id) to unsigned method to make sure that column only holds positive value and prevent erros and unexpected behaviors in queries that use that column.

jhon26
  • 313
  • 2
  • 11