Syntax error Which MySQL Data Type can be used to store Negative Number?

Which MySQL Data Type can be used to store Negative Number?



You can use TINYINT data type in MySQL to store negative number. Following is the syntax −

CREATE TABLE yourTableName
   (

   yourColumnName TINYINT
   .
   .
   .
   .
   N
   );

Let us first create a table with a column set as type TINYINT −

mysql> create table DemoTable
   (

   Number tinyint
   );
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(-10);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(-128);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values(128);
ERROR 1264 (22003): Out of range value for column 'Number' at row 1

mysql> insert into DemoTable values(127);
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values(-1);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values(14);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Number |
+--------+
|    -10 |
|   -128 |
|    127 |
|     -1 |
|     14 |
+--------+
5 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements