这个例子是工作中遇到的: 把原先的自增主键id去掉,新增auto_pk字段为自增主键,并把id作为唯一索引。
ALTER TABLE `ddd_tab`
ADD COLUMN `auto_pk` bigint(20) UNSIGNED NOT NULL FIRST ,
MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT AFTER `auto_pk`; -- 新建auto_pk,id主键为无符号
--update ad set auto_pk = id; -- 赋值
ALTER TABLE `ddd_tab`
MODIFY COLUMN `auto_pk` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST ,
MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL AFTER `auto_pk`,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`auto_pk`),
ADD UNIQUE INDEX `idx_uk_id` (`id`) ; -- id去掉自增主键改为UK,auto_pk改为自增主键