I
I
Ivseti2021-07-24 17:09:19
MySQL
Ivseti, 2021-07-24 17:09:19

How to replace id for all records in a table?

Good day!

SELECT ID, COUNT(ID) AS c FROM wp_posts GROUP BY ID HAVING c > 1


Shows me that I have 106 entries with id 0, how can I bring all the entries back to normal? do the numbering

This must be done to put the auto_increment primary key on the table. And while the DB gives me an error:
#1062 - ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '6168' for key 'PRIMARY'

When I try to enable auto_increment through the table structure.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivseti, 2021-07-26
@Ivseti

Found a solution in one of the questions. It helped, everything worked.

CREATE table wp_posts_copy LIKE wp_posts; 
ALTER TABLE wp_posts_copy ADD PRIMARY KEY (id); 
INSERT IGNORE INTO wp_posts_copy SELECT * FROM wp_posts; -- INSERT INGORE вставит только по одному экземпляру id. 
DROP TABLE wp_posts; 
RENAME TABLE wp_posts_copy TO wp_posts;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question