Home >Database >Mysql Tutorial >What happens when inserting negative values ​​into an UNSIGNED column in MySQL?

What happens when inserting negative values ​​into an UNSIGNED column in MySQL?

WBOY
WBOYforward
2023-09-03 09:53:021376browse

当向 MySQL 中的 UNSIGNED 列插入负值时会发生什么?

In MySQL, when you set a negative value to an UNSIGNED column, an error occurs. For example, let us first create a table with one UNSIGNED field −

mysql> create table UnsignedDemo
   -> (
   -> Id int UNSIGNED
   -> );
Query OK, 0 rows affected (0.79 sec)

Whenever you insert a negative value to a column Id declared as UNSIGNED, the error is as follows-

mysql> INSERT INTO UnsignedDemo VALUES(-100);
ERROR 1264 (22003): Out of range value for column 'Id' at row 1

Example

However, for the unsigned case, positive values ​​work well. This is also true in the example below. Use insert command to insert some records in the above table. The query is as follows −

mysql> INSERT INTO UnsignedDemo VALUES(100);
Query OK, 1 row affected (0.15 sec)
mysql> INSERT INTO UnsignedDemo VALUES(1000);
Query OK, 1 row affected (0.15 sec)
mysql> INSERT INTO UnsignedDemo VALUES(0);
Query OK, 1 row affected (0.11 sec)
mysql> INSERT INTO UnsignedDemo VALUES(100000000);
Query OK, 1 row affected (0.27 sec)

Use the select statement to display all records in the table. The query is as follows -

mysql> SELECT *FROM UnsignedDemo;

Output

+-----------+
| Id        |
+-----------+
|       100 |
|      1000 |
|         0 |
| 100000000 |
+-----------+
4 rows in set (0.00 sec)

The above is the detailed content of What happens when inserting negative values ​​into an UNSIGNED column in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete