Subject RE: [firebird-support] duplicates in a table with PK
Author Puigsegur, Jordi
Hi Helen,

First of all thanks for the advise on using the correct charsets. We'll
use ISO-8859-1 from now on. However I think that is not the issue of our
duplicate key problem. And I would like to find out the exact reason so
that it doesn't happen again.

> >Finally, any idea on why the duplicate values appeared ?
>
> Yes. My prime suspect would be that varchar(100)
> column and the likelihood that the database has
> stored characters that your utility programs cannot display
distinctly.

I seems this is not the problem. I can reproduce the problem by
executing this query n times:

INSERT INTO CCP_ARBOLPRECIOS (IDCATPRO, CLAVE, FECHASALIDA)
VALUES (0, 'A', '01.01.2006')

It's like there was no INDEX active and no PK on that table but if I
check with FlameRobin or isql it shows the contrary:

SQL> SHOW TABLE CCP_ARBOLPRECIOS;
IDCATPRO NUMERIC(18, 0) Not Null
CLAVE VARCHAR(100) Not Null
FECHASALIDA DATE Not Null
PRECIOS BLOB segment 80, subtype TEXT Nullable
CONSTRAINT INTEG_9:
Primary key (IDCATPRO, CLAVE, FECHASALIDA)
SQL> SHOW INDICES CCP_ARBOLPRECIOS;
RDB$PRIMARY3 UNIQUE INDEX ON CCP_ARBOLPRECIOS(IDCATPRO, CLAVE,
FECHASALIDA)
SQL>

> After that comes the (more remote) possibility
> that you were using a database that had been
> partially restored, i.e. that had restored all of
> the data but had been unable, finally, to
> activate the unique index due to some faulty
> data. The data fault itself might have arisen
> from some character set problem. In that case,
> of course, the unique constraint would not be
> operational and there would be nothing to prevent duplicate keys.

We didn't use gbak or Nbackup with this database. It was created from a
script.

> If you still have the old database, you could
> check the state of that index using gstat
> -i. Amongst other things, it will tell you whether that index was
> inactive.

C:\ >GSTAT -i d:\firebird\corrupt.fdb -user SYSDBA -password masterkey

Database "d:\firebird\corrupt.fdb"
Database header page information:
Flags 0
Checksum 12345
Generation 977871
Page size 4096
ODS version 11.0
Oldest transaction 977832
Oldest active 977868
Oldest snapshot 977868
Next transaction 977869
Bumped transaction 1
Sequence number 0
Next attachment ID 0
Implementation ID 16
Shadow count 0
Page buffers 0
Next header page 0
Database dialect 3
Creation date Mar 30, 2007 13:11:52
Attributes force write

Variable header data:
*END*


Database file sequence:
File d:\firebird\corrupt.fdb is the only file

Analyzing database pages ...
CCP_ARBOLPRECIOS (130)

Index RDB$PRIMARY3 (0)
Depth: 0, leaf buckets: 0, nodes: 0
Average data length: 0.00, total dup: 0, max dup: 0
Fill distribution:
0 - 19% = 0
20 - 39% = 0
40 - 59% = 0
60 - 79% = 0
80 - 99% = 0

CCP_ARBOLPRODUCTOS (129)

Index RDB$PRIMARY2 (0)
Depth: 2, leaf buckets: 56, nodes: 12099
Average data length: 7.70, total dup: 0, max dup: 0
Fill distribution:
0 - 19% = 0
20 - 39% = 0
40 - 59% = 13
60 - 79% = 37
80 - 99% = 6

Thanks,
Jordi.