mysql 更換主鍵,新增新主鍵欄位及主鍵初始化賦值 sql
阿新 • • 發佈:2019-01-09
順序執行以下sql語句
1.刪除表主鍵約束
ALTER TABLE mobilems_m_weex DROP PRIMARY KEY;
2.新增新主鍵欄位id
ALTER TABLE mobilems_m_weex ADD id
BIGINT(20) NOT NULL COMMENT ‘主鍵’ FIRST;
3.id賦值
#顯示行號的sql
SELECT
@r := @r + 1 AS row_num,a.*
FROM mobilems_m_weex a, (SELECT @r := 0) b;
把已經存在的行號當作主鍵賦值給主鍵
UPDATE mobilems_m_weex mmw1 INNER JOIN (SELECT
@r := @r + 1 AS row_num,
mmw2.bundle_id,
mmw2.lowest_support_version
FROM mobilems_m_weex mmw2, (SELECT @r := 0) b) tb
ON mmw1.bundle_id = tb.bundle_id AND mmw1.lowest_support_version = tb.lowest_support_version SET mmw1.id = tb.row_num;
5.設定主鍵
ALTER TABLE mobilems_m_weex
ADD PRIMARY KEY (`id`);