Difference between Signed and Unsigned in MySQL
Start your free 7-days trial now!
UNSIGNED: only stores positive numbers (or zero).
SIGNED: can store negative numbers.
Below is a table of the range of values each integer type can store:
Type | Storage (Bytes) | Min Value Signed | Min Value Unsigned | Max Value Signed | Max Value Unsigned |
|---|---|---|---|---|---|
| 1 | -128 | 0 | 127 | 255 |
| 2 | -32768 | 0 | 32767 | 65535 |
| 3 | -8388608 | 0 | 8388607 | 16777215 |
| 4 | -2147483648 | 0 | 2147483647 | 4294967295 |
| 8 | - 2^63 | 0 | 2^63 - 1 | 2^64 - 1 |
Notice that with UNSIGNED, you're essentially giving yourself twice as much space for the integer since you explicitly specify you don't need negative numbers.
When deciding whether to use SIGNED or UNSIGNED for a field, ask yourself wether this field will ever contain a negative value. If the answer is no, then you want an UNSIGNED data type. A common use case for UNSIGNED is for auto-increment id fields in a table.
Examples
To specify an UNSIGNED auto-increment id column:
CREATE TABLE info ( id INT UNSIGNED AUTO_INCREMENT, name VARCHAR(20), age INT, hobby VARCHAR(20), PRIMARY KEY (id));
Query OK, 0 rows affected (0.03 sec)
To specify a SIGNED column profit to represent profit on investments:
CREATE TABLE stock ( stock_code INT, stock_name VARCHAR(20), profit INT SIGNED, PRIMARY KEY (stock_code));
Query OK, 0 rows affected (0.01 sec)
As the profit column should be able to take negative values to represent losses on investments we have specified the column as SIGNED.