Computer >> Computer tutorials >  >> Programming >> MySQL

How to use MySQL decimal?


MySQL decimal data type can be used to store the exact numerical value. The syntax of DECIMAL data type.

yourColumnName Decimal(integerValue,intgerValue);

Example of DECIMAL data type.

mysql> create table EmployeeInformation
   -> (
   -> EmpId int auto_increment primary key,
   -> EmpName varchar(200),
   -> EmpSalary DECIMAL(6.2)
   -> );
Query OK, 0 rows affected (0.54 sec)

Whenever we insert a value in “EmpSalary” column greater than 6 digits, it raise an error. The error is as follows −

mysql> insert into EmployeeInformation(EmpName,EmpSalary) values('John',6999999.50);
ERROR 1264 (22003): Out of range value for column 'EmpSalary' at row 1

Inserting value in “EmpSalary” column with exact number of digits.

mysql> insert into EmployeeInformation(EmpName,EmpSalary) values('John',6999.50);
Query OK, 1 row affected, 1 warning (0.15 sec)

mysql> insert into EmployeeInformation(EmpName,EmpSalary) values('John',69999.50);
Query OK, 1 row affected, 1 warning (0.20 sec)

To display all records.

mysql> select *from EmployeeInformation;

The following is the output.

+-------+---------+-----------+
| EmpId | EmpName | EmpSalary |
+-------+---------+-----------+
|     1 | John    |      7000 |
|     2 | John    |     70000 |
+-------+---------+-----------+
2 rows in set (0.00 sec)