Duplicate records in tables

Hi there,

I have a couple of tables that have duplicate records.
I do NOT want to delete the duplicate records.
All I want to do, is add a primary key to the table so that the records can be uniquely identified.

What would be the easiest way to add a Primary Key (or a field) to the tables without loosing the duplicate records? (hope this is possible).
A query, small program would really help.

If I add a field to the table and try to make it a primary key,
Interbase manager generates an error.

The table have about 3000 000 records.
Im using interbase 5.1 and 5.5.
Programming in Borland C++ Builder 5


Who is Participating?
kacorConnect With a Mentor retiredCommented:
Hi morees,

1. create domains if needed

2. create NewTable with the existing structure
    create table NewTable
    ID_field       integer NOT NULL, //this is your new column for primary key
    Field2 FieldType2,
    ... etc
3. create generator
    create generator xxxx;
4. create trigger      //to increment the PK

    set term ^ ;
    CREATE TRIGGER xxxx for NewTable
            NEW.ID_Field = GEN_ID( xxxx, 1 );
    set term ;
5. copy your data to the new table

   insert into NewTable
        select *
        from OldTable

I hope it helps


moreesAuthor Commented:
Thanks for the reply kacor...

I tried what you said I must end then I get the following error:

"Unsuccessful execution caused by a system error that precludes
successful execution of subsequent statements.
internal gds software consistency check (wrong record length (183))."

In the table there are 2 records which are identical.
All the field values are exactly the same. for example

# | code | price ...
1  | 123  | 12.45 ...
1  | 123  | 12.45 ...

There are a couple of records like this.

I think this might be why I get the error.
Is there a way around this (I hope so)?


Hi moores,

You know in a database the system permits no record duplication because every record must differ - min. one character - from the other. I told therefore to insert a new field which would be unique - so every record could differ each from other. What I can imagine: you have to make a stored procedure to search for duplicated record in the old database/table and if this search found something duplicated, in a given textual column places a suffix for example "dummy" but only for the second/third/etc. "copy". If it would be repeated more time this would be added twice, threetimes, etc. so every record could differ (eg. every record would be unique in this sense). After this tuning you could copy the "old" (but renewed) table into the new table, thereafter you can delete from this tuned column every occurence of the added word (in this case "dummy"). This is the only possibility I mean. May be some more experienced gurus could suggest something better, now this was what I could imagine.

have success!


Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

but of course I am waiting your message about

As now I read the complet question and the comments, I see that you wanted to be every record unique. Sorry for the mistake. I felt this was my idea :-)))))

again sorry  Janos
moreesAuthor Commented:

you know this error that I got when I tried your lolution

(Unsuccessful execution caused by a system error that precludes
successful execution of subsequent statements.
internal gds software consistency check (wrong record length (183)).")   <-- this error,

well I tried to browse through the records and the same error came up just by going through the records.
I tried doing a backup and recovery but got the same error.
So I had to recover an old database and tried your solution again. I had to add a 'dummy' column in the old table just so that the column count matched in the 2 tables and geuss what it worked.
Thanx for 'what should have been a quick solution', but took longer because of a stuffed upped table and a poor database.

hi morees,

the pleasure is mine

just a comment on that - same can be achieved with adding an ID collumn to same table
Just add an integer collumn, set unique values and then make it primary keys.
you can achieve it with

CREATE GENERATOR gen_yourtable_id;

update yourtable
set id = gen_id(gen_yourtable_id,1)

Update in interbase is performed record by record like iterating with cursor (delete also) so this will work.

Bojidar Alexandrov
moreesAuthor Commented:
I tried what you said and it worked aswell.
The only difference was that I had to set the new field to be the primary field after I updated the table, and not before the update.
It generates an error if you try to make it a primary field before the update


Hi Bojidar,

I offered this solution because in his question morees said, he tried to insert a new column


that's right - may be you have to read my answer again?
Sorry Bojidar, it escaped my attention

wbr Janos
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.