我有一个city_id列,其中包含 varchar(255)和空值。我想将其更改为大整数。
我将使用:
ALTER TABLE Customer
ALTER COLUMN CITY_ID BIGINT
我会丢失或破坏任何数据吗?
答案 0 :(得分:2)
您可以尝试添加一个新的大整数列作为中间步骤:
-- add new column
ALTER TABLE Customer ADD COLUMN CITY_ID_NEW BIGINT;
-- populate new column
UPDATE Customer
SET CITY_ID_NEW = CAST(CITY_ID AS UNSIGNED);
-- drop old column and rename new column
ALTER TABLE Customer DROP COLUMN CITY_ID;
ALTER TABLE Customer CHANGE COLUMN CITY_ID_NEW CITY_ID BIGINT NULL DEFAULT NULL;
我在上面假设您的整数值都是正数,即它们没有符号。如果需要带符号的整数,则将更新中的强制类型转换更改为SIGNED
。