mysql新增一列为主键

mysql新增一列为主键

由于一次疏忽在建表的时候忘记加上主键了, 但是目前来说表里面又有数据了,所以不能删表重建,所以需要新加一列主键

然后我就新加一列,并且为auto_increment,然后设置为主键,如下:

alter table ti_ares_ztfi_bukvsckks
	add table_id int auto_increment;


alter table ti_ares_ztfi_bukvsckks
	add constraint ti_ares_ztfi_bukvsckks_pk
		primary key (table_id);

结果报了个错:

[42000][1075] Incorrect table definition; there can be only one auto column and it must be defined as a key

后来google之后发现不能这么做 , 所以在这里记一下正确的做法:

alter table ti_ares_ztfi_bukvsckks add COLUMN id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY first;

原文地址:https://www.cnblogs.com/luozhiyun/p/10196523.html