- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Should I store a field PRICE as an int or as a float in the database?
You do not need to store a field PRICE as an int or as float in the database. For this, you can set the DECIMAL()..
Most of the time integers can be used to represent the float point numbers and these integers are internally cast into DECIMAL() data type. Therefore, if you have field PRICE then always use DECIMAL() data type. The syntax is as follows −
Here, M represents the ‘TotalNumberOfDigit’ and D represents the ‘Number OfDigitAfterDecimalPoint’.
To understand the above concept, let us create a table with field PRICE as DECIMAL data type. The query is as follows −
mysql> create table Decimal_Demo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRICE DECIMAL(18,2), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.63 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into Decimal_Demo(PRICE) values(99999999999999.99); Query OK, 1 row affected (0.11 sec) mysql> insert into Decimal_Demo(PRICE) values(10000000000000.99); Query OK, 1 row affected (0.36 sec)
Now you can display all records from the table using a select statement. The query is as follows −
mysql> select *from Decimal_Demo;
The following is the output −
+----+-------------------+ | Id | PRICE | +----+-------------------+ | 1 | 99999999999999.99 | | 2 | 10000000000000.99 | +----+-------------------+ 2 rows in set (0.00 sec)
- What should I do? Select int as currency or convert int to currency format in MySql?
- How should I pass a matplotlib object through a function; as Axis, Axes or Figure?
- How should I store data into my Mysql database, what type should I assign to a column storing salt values?
- Ordering string as a number in a database?
- As an HR of a company, what kind of attitude should I carry at work?
- How do I format a number as decimal to store it in MySQL?
- How can I store ‘0000-00-00’ as a date in MySQL?
- How to parse a string to float or int in python?
- How can I store the fixed length string as well as variable length string in the same MySQL table?
- Can a Java array be declared as a static field, local variable or a method parameter?
- Should a constructor always have the same name as the class in java?
- How can I get the output of a Matplotlib plot as an SVG?
- Performing a MySQL LIKE comparison on an INT field?
- Particular field as result in MongoDB?