26

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?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
ValleyDigital
  • 1,460
  • 4
  • 21
  • 37

5 Answers5

56
ALTER TABLE mytable MODIFY COLUMN mycolumn newtype

example:

ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
13

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).

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • I'm reading this because I fell for the bottom part. Read the docs! Thanks for explaining that aspect of decimal. I'm sure it catches a lot of people out. – Lightbulb1 May 15 '20 at 10:28
4

It's not a matter of 'UPDATE' it's a matter of changing your table's structure. For that, use ALTER TABLE with MODIFY clause:

ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);

sqlfiddle demo

Filipe Silva
  • 21,189
  • 5
  • 53
  • 68
1

use CHANGE

ALTER TABLE table_name CHANGE OLD_COLUMN_NAME OLD_COLUMN_NAME datatype;

an example

ALTER TABLE table_name CHANGE price price decimal(4,2);
denny
  • 2,084
  • 2
  • 15
  • 19
-1

You can change it dynamically:-

ALTER TABLE ".$row["table_name"]." MODIFY ".$row1["Field"]." decimal(".$rdataType[0].",".$increaseValue.")

For more info:

Repository

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Nazmul
  • 1
  • 1
  • 4