
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Prevent negative numbers in MySQL?
To prevent negative numbers in MySQL, you need to use INT UNSIGNED. Let’s say you created a table with a column as int i.e. UserGameScores here
mysql> create table preventNegativeNumberDemo - > ( - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserName varchar(20), - > UserGameScores int - > ); Query OK, 0 rows affected (1.74 sec)
Now if you need to prevent negative numbers in it, modify the same column with INT UNSIGNED
mysql> alter table preventNegativeNumberDemo modify column UserGameScores INT UNSIGNED NOT NULL; Query OK, 0 rows affected (3.32 sec) Records: 0 Duplicates: 0 Warnings: 0
Now check the table description once again.
The query is as follows
mysql> desc preventNegativeNumberDemo;
The following is the output
+----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------------+------+-----+---------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserName | varchar(20) | YES | | NULL | | | UserGameScores | int(10) unsigned | NO | | NULL | | +----------------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
Now if you try to insert negative number for the UserGameScores column then MySQL gives an error since we have set it as INT UNSIGNED. Let us insert some values including negative number
mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Larry',0); Query OK, 1 row affected (1.20 sec) mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Mike',-1); ERROR 1264 (22003): Out of range value for column 'UserGameScores' at row 1 mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Sam',-100); ERROR 1264 (22003): Out of range value for column 'UserGameScores' at row 1 mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('John',100); Query OK, 1 row affected (0.84 sec) mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Bob',200); Query OK, 1 row affected (0.48 sec)
Look at the above error when we try to insert negative value.
Now display all records from the table using select statement. Only the positive numbers will get inserted
mysql> select *from preventNegativeNumberDemo;
The following is the output
+--------+----------+----------------+ | UserId | UserName | UserGameScores | +--------+----------+----------------+ | 1 | Larry | 0 | | 2 | John | 100 | | 3 | Bob | 200 | +--------+----------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Modulus of Negative Numbers in C
- Negative Binary Numbers\n
- What is negative numbers ?
- How to prevent duplicate INSERT in MySQL?
- Reversing negative and positive numbers in JavaScript
- How to hide negative numbers in Excel?
- Make array numbers negative JavaScript
- Distinguish positive and negative numbers.
- What are the negative numbers?
- Can negative numbers be prime?
- How to prevent duplicate rows in MySQL INSERT?
- Prevent having a zero value in a MySQL field?
- Removal of negative numbers from an array in Java
- Python program to print negative numbers in a list
- How to change negative numbers to positive in Excel?

Advertisements