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

我是研究僧i 提交于 2019-12-21 07:14:41

问题


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?

I can't seem to get the syntax right.

Thank you very much.


回答1:


ALTER TABLE mytable MODIFY COLUMN mycolumn newtype

example:

ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)



回答2:


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




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/19773164/mysql-how-do-i-update-the-decimal-column-to-allow-more-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!