What is unsigned in MySQL?

Unsigned allows us to enter positive value; you cannot give any negative number. Let us create a table to understand unsigned in MySQL. To create a table, we will use the CREATE command.

Let us create a table −

mysql> CREATE table UnsignedDemo
-> (
-> id int unsigned
-> );
Query OK, 0 rows affected (0.61 sec)

After that I will insert only positive values. Let us insert some records −

mysql> INSERT into UnsignedDemo values(124);
Query OK, 1 row affected (0.09 sec)

mysql> INSERT into UnsignedDemo values(78967);
Query OK, 1 row affected (0.14 sec)

I am displaying all the records with the help of SELECT command −

mysql> SELECT * from UnsignedDemo;

The following is the output

| id    |
| 124   |
| 78967 |
2 rows in set (0.00 sec)

Now, we will try to insert only negative values. But while doing this, we will get the following error, since the column ‘id’ is unsigned −

mysql> INSERT into UnsignedDemo values(-124);
ERROR 1264 (22003): Out of range value for column 'id' at row 1