
- 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
Add a positive integer constraint to an integer column in MySQL?
You need to use unsigned for this because it won’t allow you to enter a negative number.
The syntax is as follows
CREATE TABLE yourTableName ( yourColumnName INT UNSIGNED );
To understand the concept, let us create a table. The query to create a table is as follows
mysql> create table OnlyPositiveValue - > ( - > Marks int UNSIGNED - > ); Query OK, 0 rows affected (0.58 sec)
Before inserting data in the table, use the below query.
The query is as follows
mysql> SET @@SESSION.sql_mode = 'STRICT_TRANS_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec)
Now if you will add a negative number in the INSERT command, the following error would be visible
mysql> insert into OnlyPositiveValue values(-10); ERROR 1264 (22003): Out of range value for column 'Marks' at row 1 mysql> insert into OnlyPositiveValue values(-100); ERROR 1264 (22003): Out of range value for column 'Marks' at row 1
Let us insert positive numbers.
The query is as follows
mysql> insert into OnlyPositiveValue values(0); Query OK, 1 row affected (0.17 sec) mysql> insert into OnlyPositiveValue values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into OnlyPositiveValue values(100); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from OnlyPositiveValue;
The following is the output displaying positive numbers
+-------+ | Marks | +-------+ | 0 | | 10 | | 100 | +-------+ 3 rows in set (0.00 sec)
- Related Articles
- How to add NOT NULL constraint to an already created MySQL column?
- (a) Write a negative integer and a positive integer whose sum is $-5$.(b) Write a negative integer and a positive integer whose difference is $-3$.
- How to add not null constraint to existing column in MySQL?
- Find integer within +/- 1 from a column in MySQL
- What will be the result when adding a positive integer to a negative integer?
- What happens when we multiply a negative integer with a positive integer?
- Generate an integer sequence in MySQL?
- MySQL Order By date column and integer column, but specify ordering rules of integer column? Is it possible?
- Positive integer square array JavaScript
- How to negate the positive elements of an integer array in C#?
- ......................... is neither a positive nor a negative integer.
- Datetime to Integer in MySQL?
- Program to express a positive integer number in words in C++
- Java program to reverse bits of a positive integer number
- Python program to reverse bits of a positive integer number?

Advertisements