我有一个int(10)类型为unsigned的price列,但是由于某些原因,我需要将列的类型更改为decimal(3,2),如何做到这一点而又不会丢失已经存储的数据int(10)?
ALTER TABLE packages MODIFY price decimal(3,2)
但是此命令会将所有值更改为9,99吗?
我该如何减轻痛苦?
答案 0 :(得分:1)
您的价格可能会下限到您指定的最大允许小数。试试:
DELIMITER $$
CREATE PROCEDURE statement(IN dynamic_statement TEXT)
BEGIN
SET @dynamic_statement := dynamic_statement;
PREPARE prepared_statement FROM @dynamic_statement;
EXECUTE prepared_statement;
DEALLOCATE PREPARE prepared_statement;
END;
DELIMITER ;
SET @var_digit_length :=
(SELECT MAX(CEIL(LOG(10, ABS(price))))
FROM packages)
;
SET @var_precision := 2;
CALL statement(CONCAT('
ALTER TABLE packages MODIFY price decimal(',
(@var_digit_length + @var_precision), ',', @var_precision, ');
'));
DROP PROCEDURE statement;