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
Release: 2023-09-03 09:53:02
forward
1404 people have browsed it

当向 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)
Copy after login

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
Copy after login

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)
Copy after login

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

mysql> SELECT *FROM UnsignedDemo;
Copy after login

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template