Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRGDZ-000466-R6 for pgsql-admin@arkaria.postgresql.org; Wed, 22 Apr 2020 14:19:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRGDY-00037n-Pi for pgsql-admin@arkaria.postgresql.org; Wed, 22 Apr 2020 14:19:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRGDY-00037f-FB for pgsql-admin@lists.postgresql.org; Wed, 22 Apr 2020 14:19:12 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRGDV-0000rg-NC for pgsql-admin@lists.postgresql.org; Wed, 22 Apr 2020 14:19:12 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id 4583EBE06C2; Wed, 22 Apr 2020 16:19:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at rs.plausibolo.de Received: from rs.plausibolo.de ([127.0.0.1]) by localhost (h2367442.stratoserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 34dPSyB7QXot; Wed, 22 Apr 2020 16:19:04 +0200 (CEST) Received: from [192.168.178.194] (i577BCF94.versanet.de [87.123.207.148]) by rs.plausibolo.de (Postfix) with ESMTPSA id A774CBE0152; Wed, 22 Apr 2020 16:19:04 +0200 (CEST) Date: Wed, 22 Apr 2020 16:18:46 +0200 User-Agent: K-9 Mail for Android In-Reply-To: <671E1525-61C8-4A7B-AB1F-F936BF22ED49@crazybean.net> References: <2A91BEF8171A5349931391E0C721CC5396920741@CPEMS-KPN301.KPNCNL.LOCAL> <671E1525-61C8-4A7B-AB1F-F936BF22ED49@crazybean.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----CXKPR2ZEQ7FFEPVCFTPSRIZK2Z7RXP" Content-Transfer-Encoding: 7bit Subject: Re: Composite primary key duplicate records - Issue In PostgreSQL But not in Oracle To: pgsql-admin@lists.postgresql.org,Rui DeSousa ,"David G. Johnston" CC: soumik.bhattacharjee@kpn.com,pgsql-admin ,Pgsql-admin From: Holger Jakobs Message-ID: <256CA957-E271-4EBB-98FF-1B337AFD9F17@jakobs.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------CXKPR2ZEQ7FFEPVCFTPSRIZK2Z7RXP Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Oracle DATE is actually a TIMESTAMP(0)=20 Am 22=2E April 2020 16:16:07 MESZ schrieb Rui DeSousa : > > >> On Apr 22, 2020, at 10:09 AM, David G=2E Johnston > wrote: >>=20 >> On Wed, Apr 22, 2020 at 6:54 AM > wrote: >> Hi Experts, >>=20 >> =20 >>=20 >> We are migrating Oracle to PostgreSQL and facing a challenge related >to constrains=2E >>=20 >> =20 >>=20 >> In Oracle database there are lot of table consists of composite >primary key which having duplicate records=2E >>=20 >> =20 >>=20 >> Example >>=20 >> =20 >>=20 >> Oracle table : --CONSTRAINT "TTT_PRX" PRIMARY KEY ("COL1", "COL2") =E2= =80=93 >UNIQUE =C3=A0 Here in Oracle there is no issue >> =20 >>=20 >> In PostgreSQL if I want to add the constraint post migration it >throws error as duplicate record for COL1 (which is a number) as per >business need, COL2 is date which is unique always=2E >> =20 >>=20 >> alter table table_name add constraint ttt_prx unique(COL1, COL2); >>=20 >> =20 >>=20 >> COL1 >>=20 >> COL2 >>=20 >> Count >>=20 >> 102022194 >>=20 >> 21-9-2019 >>=20 >> 1 >>=20 >> 102022194 >>=20 >> 30-9-2019 >>=20 >> 1 >>=20 >> 102022194 >>=20 >> 30-9-2019 >>=20 >> 1 >>=20 >> =20 >>=20 >> =20 >>=20 >> Is there a way to handle this in PostgreSQL? >>=20 >>=20 >>=20 >>=20 >> If Oracle is telling you that having two records >(102022194,30-9-2019) is not a violation of the defined unique >constraint over those two columns it is wrong=2E PostgreSQL is handling >this correctly=2E >>=20 >> Maybe you had a full-on timestamp in Oracle but in moving it to >PostgreSQL you mis-typed it as a date so the difference in the time >portion of the two records has been lost? >>=20 >> David J=2E > >Correct, the issue is the data in Oracle=2E The date field internally >stores down to the second; not a great idea to use date in as primary >key on Oracle without truncating it first: > >https://asktom=2Eoracle=2Ecom/pls/asktom/f?p=3D100:11:0::::P11_QUESTION_I= D:47566188181366 --=20 Holger Jakobs, Bergisch Gladbach +49 178 9759012 - sent from mobile, therefore short - ------CXKPR2ZEQ7FFEPVCFTPSRIZK2Z7RXP Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Oracle DATE is actually a = TIMESTAMP(0)


Am 22=2E April 2020 16:= 16:07 MESZ schrieb Rui DeSousa <rui@crazybean=2Enet>:


On Apr 22, 2020, at 10:09 AM, David G=2E Johnston <david=2Eg=2Ejohnsto= n@gmail=2Ecom> wrote:

<= div class=3D"">
On Wed, Apr 22, 2020 at 6:54 AM <soumik=2Ebhattacharjee@kpn=2E= com> wrote:

Hi Experts,

 

We are migrating Oracle to PostgreSQL and facing a ch= allenge related to constrains=2E=

 

In Oracle database there are lot of table consists of comp= osite primary key which having duplicate records=2E

 

Example

<= span lang=3D"EN-GB" class=3D""> 

  • Oracle  table : --CONSTRAINT "TTT_PRX" PRIMARY KEY ("COL1", "COL2") = =E2=80=93 UNIQUE <= span lang=3D"EN-GB" style=3D"font-size: 9pt; font-family: Wingdings;" class= =3D"">=C3=A0 <= span lang=3D"EN-GB" class=3D"">Here in Oracle there is no issue<= span lang=3D"EN-GB" style=3D"font-size: 9pt;" class=3D"">=

 

  • In PostgreSQL if I want to add the constraint post migrat= ion it throws error as duplicate record for COL1 (which is a number) as per= business need, COL2 is date which is unique always=2E

 

alter table table_name add constraint ttt_prx unique(= COL1,<= b class=3D""> COL2);

 

<= td width=3D"85" nowrap=3D"" valign=3D"bottom" style=3D"width: 63=2E85pt; bo= rder-top-width: 1pt; border-style: solid solid solid none; border-top-color= : windowtext; border-right-width: 1pt; border-right-color: windowtext; bord= er-bottom-width: 1pt; border-bottom-color: windowtext; background-color: ye= llow; padding: 0cm 3=2E5pt; height: 7=2E9pt; background-position: initial i= nitial; background-repeat: initial initial;" class=3D"">

Count

COL1=

COL2

102022194

21-9-2019

1

102022194=

30-9-2019

1

102022194

30-9-2019<= u class=3D"">

1

<= b class=3D""> 

 =

Is= there a way to handle this in PostgreSQL?<= /u>



If Oracle is telling = you that having two records (102022194,30-9-2019) is not a violation of the= defined unique constraint over those two columns it is wrong=2E  Post= greSQL is handling this correctly=2E


David J= =2E

Correct, the issue is the data in Oracle=2E  The date field internal= ly stores down to the second; not a great idea to use date in as primary ke= y on Oracle without truncating it first:


--
Holger Jakobs, Bergisch Gladbach+49 178 9759012
- sent from mobile, therefore short -
------CXKPR2ZEQ7FFEPVCFTPSRIZK2Z7RXP--