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

Thanks


moreesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kacorretiredCommented:
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
    CONSTRAINT PK_ID_Field PRIMARY KEY (ID_field)
    );    
3. create generator
    create generator xxxx;
4. create trigger      //to increment the PK

    set term ^ ;
    CREATE TRIGGER xxxx for NewTable
    ACTIVE BEFORE INSERT POSITION 0
    AS
      BEGIN
            NEW.ID_Field = GEN_ID( xxxx, 1 );
      END
     ^
    set term ;
5. copy your data to the new table

   insert into NewTable
        select *
        from OldTable

I hope it helps

wbr

Janos
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)?

Cheers

0
kacorretiredCommented:
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!

wbr

Janos
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

kacorretiredCommented:
but of course I am waiting your message about

Janos
0
kacorretiredCommented:
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
0
moreesAuthor Commented:
kacor,

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.

Cheers
0
kacorretiredCommented:
hi morees,

the pleasure is mine

Janos
0
BAlexandrovCommented:
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
0
moreesAuthor Commented:
BAlexandro
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

Thanks

0
kacorretiredCommented:
Hi Bojidar,

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

wbr

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

wbr Janos
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.