迁移后,我有多个没有索引或主键的表。某些表中有 9149 个条目,大约有 10 个新条目被赋予 id = 0。
由于多个具有相同 id 的条目,我无法提供主键,由于没有主键,我无法通过单击进行编辑。我不知道该怎么办。
现有的最高帖子 ID 号是 9149。
有人可以给我用从 9150 向上的序列号重写这些。
迁移后,我有多个没有索引或主键的表。某些表中有 9149 个条目,大约有 10 个新条目被赋予 id = 0。
由于多个具有相同 id 的条目,我无法提供主键,由于没有主键,我无法通过单击进行编辑。我不知道该怎么办。
现有的最高帖子 ID 号是 9149。
有人可以给我用从 9150 向上的序列号重写这些。
在这里,我使用一个变量来保存最大 id,然后使用该变量更新 id = 0 的行
SET @rownum = (SELECT MAX(id) FROM test)
UPDATE test SET id =
(SELECT @rownum := (@rownum + 1))
WHERE id = 0
This should be as simple as :
ALTER TABLE wp_posts ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
This command adds a column named id
as the first column in the table, and makes it the primary key. As the column has auto increment option enabled, MySQL will automatically assign a number to each record.
Here is a demo on DB Fiddle that demonstrates the principle :
create table mytable (val int);
insert into mytable values(50);
insert into mytable values(50);
insert into mytable values(51);
alter table mytable add id int not null auto_increment primary key first;
select * from mytable;
| id | val |
| --- | --- |
| 1 | 50 |
| 2 | 50 |
| 3 | 51 |