I have a table in CockroachDB, I have populated the data into the table before applying the constraints to set the primary key, and because some of INSERT statement failed through the data-loading phase, some of the rows are loaded into the table more than one time by mistake.
The constraint I want to apply is:
CREATE UNIQUE INDEX ON "MyDB"."MyTable" ("Row_ID");
But as the duplicate data is already loaded into the table, I get the following error:
pq: multiple primary keys for table "MyTable" are not allowed
I have check to see if actually there are some duplicated rows with the following query:
SELECT
Row_ID,
COUNT(Row_ID) as id
FROM
MyTable
GROUP BY
Row_ID
HAVING
COUNT(Row_ID) > 1;
And the query showed there are some duplicate rows.
What is the best way to remove the duplicate rows in CockroachDB?