Tuesday, June 5, 2012

Timestamp column as part of composite Primary key



The precision of systimestamp is platform dependent. 
On most Unix platforms it's microseconds (10-6), while on Windows this is Milliseconds (10-3). 

For example:

On Linux Environment:

SQL> create table test(c1 timestamp(6));
Table created.
SQL> begin
  2  for i in 1..10 loop
  3  insert into test values(systimestamp);
  4  end loop;
  5  end;
  6  /
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> select * from test;
C1
---------------------------------------------------------------------------
04-JUN-12 07.04.42.198435 PM
04-JUN-12 07.04.42.276114 PM
04-JUN-12 07.04.42.276165 PM
04-JUN-12 07.04.42.276193 PM
04-JUN-12 07.04.42.276221 PM
04-JUN-12 07.04.42.276248 PM
04-JUN-12 07.04.42.276274 PM
04-JUN-12 07.04.42.276302 PM
04-JUN-12 07.04.42.276329 PM
04-JUN-12 07.04.42.276355 PM

10 rows selected.

On Windows Environment:

The milliseconds value in timestamp is same in all the rows:

SQL> select * from test;

C1
----------------------------
04-JUN-12 07.02.38.981000 PM
04-JUN-12 07.02.38.981000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM
04-JUN-12 07.02.38.982000 PM

10 rows selected.

Due the same value in timestamp column, only the first row will be inserted in the table, and the rest would be non-unique and gets rejected. The problem is due to Windows limitations.

The workaround could be to add one more column to the composite primary key, and link the new column with a sequence to make it unique.

No comments:

Post a Comment