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?
它必须是十进制(4,2)才能允许4位数。由于我已经创建了它,将十进制值更新为十进制(4,2)的最简单方法是什么?或者我是否需要完全删除该列,并使用正确的数字重新创建它?
I can't seem to get the syntax right.
我似乎无法正确使用语法。
Thank you very much.
非常感谢你。
4 个解决方案
#1
21
ALTER TABLE mytable MODIFY COLUMN mycolumn newtype
example:
例:
ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)
#2
7
Just ALTER TABLE
with the MODIFY
command:
只是带有MODIFY命令的ALTER TABLE:
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
).
这将允许2位小数和2位完整数字(最多99.99)。如果你想要4个完整数字,请改用6,2(最多允许9999.99)。
#3
2
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:
这不是'更新'的问题,而是改变你的桌子结构。为此,使用带有MODIFY子句的ALTER TABLE:
ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);
sqlfiddle演示
#4
0
use CHANGE
使用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);
#1
21
ALTER TABLE mytable MODIFY COLUMN mycolumn newtype
example:
例:
ALTER TABLE YourTableNameHere MODIFY COLUMN YourColumnNameHere decimal(4,2)
#2
7
Just ALTER TABLE
with the MODIFY
command:
只是带有MODIFY命令的ALTER TABLE:
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
).
这将允许2位小数和2位完整数字(最多99.99)。如果你想要4个完整数字,请改用6,2(最多允许9999.99)。
#3
2
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:
这不是'更新'的问题,而是改变你的桌子结构。为此,使用带有MODIFY子句的ALTER TABLE:
ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);
sqlfiddle演示
#4
0
use CHANGE
使用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);