MySQL - 如何更新小数列以允许更多数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19773164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:20:10  来源:igfitidea点击:

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

mysqlsql-updatedecimalalter-table

提问by ValleyDigital

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

我是 MySQL 的初学者,我不小心创建了一个表,其中有一列名为

(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?

它需要decimal(4,2)允许4位数字。由于我已经创建了它,将十进制值更新为 的最简单方法是什么decimal(4,2)?或者我是否需要完全删除该列,并使用正确的数字重新创建它?

回答by Eduardo Dennis

ALTER TABLE mytable MODIFY COLUMN mycolumn newtype

example:

例子:

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

回答by h2ooooooo

Just ALTER TABLEwith the MODIFYcommand:

只需ALTER TABLE使用以下MODIFY命令:

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,2instead (which would allow up to 9999.99).

这将允许 2 个小数和 2 个完整数字(最多99.99)。如果您想要 4 个完整数字,请6,2改用(最多允许9999.99)。

回答by Filipe Silva

It's not a matter of 'UPDATE' it's a matter of changing your table's structure. For that, use ALTER TABLEwith MODIFYclause:

这不是“更新”的问题,而是更改表结构的问题。为此,将ALTER TABLEMODIFY子句一起使用:

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

sqlfiddle demo

sqlfiddle demo

回答by denny

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