Display the warning message when a FLOAT value is inserted into DECIMAL in MySQL?

MySQLMySQLi Database

You can create a temporary table with data type DECIMAL to get a warning when a float value is inserted into an int column. Display the same warning using SHOW WARNINGS.

Let us create a table to understand. The query is as follows to create a table.

mysql> create temporary table WarningDemo
   -> (
   -> Value DECIMAL
   -> );
Query OK, 0 rows affected (0.13 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into WarningDemo values(9.80);
Query OK, 1 row affected, 1 warning (0.03 sec)

Here we are getting a warning. Let us check the warning using SHOW command. The syntax is as follows −

SHOW WARNINGS;

The query is as follows −

mysql> show warnings;

Output

+-------+------+--------------------------------------------+
| Level | Code | Message                                    |
+-------+------+--------------------------------------------+
| Note  | 1265 | Data truncated for column 'Value' at row 1 |
+-------+------+--------------------------------------------+
1 row in set (0.00 sec)

Display all records from the table using a select statement. The query is as follows −

mysql> select *from WarningDemo;

Output

+-------+
| Value |
+-------+
|    10 |
+-------+
1 row in set (0.00 sec)
raja
Published on 19-Mar-2019 06:50:40
Advertisements