Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How do I update the decimal column to allow more digits?

I'm a beginner in MySQL, and I accidentally created a table with a column named

(price decimal(2,2));

It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to decimal(4,2)? Or do I need to drop that column completely, and re-create it with the correct numbers?

like image 896
ValleyDigital Avatar asked Nov 04 '13 17:11

ValleyDigital


People also ask

How do I increase the length of a decimal in SQL?

Just put decimal(precision, scale) , replacing the precision and scale with your desired values.

What is the range of decimal in MySQL?

It has a range of 1 to 65. D is the number of digits to the right of the decimal point (the scale).

How do I get 2 decimal places in MySQL?

The ROUND() function rounds a number to a specified number of decimal places.


2 Answers

ALTER TABLE mytable MODIFY COLUMN mycolumn newtype

example:

ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)
like image 184
Eduardo Dennis Avatar answered Sep 30 '22 02:09

Eduardo Dennis


Just ALTER TABLE with the MODIFY command:

ALTER TABLE `table` MODIFY `price` DECIMAL(4,2)

This would allow for 2 decimals and 2 full numbers (up to 99.99). If you want 4 full numbers, use 6,2 instead (which would allow up to 9999.99).

like image 12
h2ooooooo Avatar answered Sep 30 '22 00:09

h2ooooooo