Avatar of emi_sastra
emi_sastra
 asked on

MULTI COLUMNS FOREIGN KEY

Hi All,

I have two tables :

1. TableA
    PurchaseOrderNo
    ItemNo
    Qty_A
    Unit_B

2. TableB
    PurchaseOrderNo
    ItemNo_A
    ItemNo_B
    Qty_B
    Unit_b
    ......

How could I set foreign key TableB to TableA for PurchaseOrderNo and ItemNo_A ?

Thank you.
Microsoft SQL Server 2005

Avatar of undefined
Last Comment
emi_sastra

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jogos

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
emi_sastra

ASKER
Hi Jogos,

ALTER TABLE TDPOJUALBRGCUSTOMER2012 ADD CONSTRAINT FK_TDPOJUALBRGCUSTOMER2012_Nomor_OA, BarangCode  FOREIGN KEY(Nomor_OA, BarangCode) REFERENCES TDPOJUAL2012(Nomor_OA, BarangCode)

What's wrong with my code ?

TDPOJUALBRGCUSTOMER2012_Nomor_OA, BarangCode.

Should be used what code ?

Thank you.
emi_sastra

ASKER
I also try below :

ALTER TABLE TDPOJUALBRGCUSTOMER2012 ADD CONSTRAINT FK_TDPOJUAL2012_TDPOJUALBRGCUSTOMER2012  FOREIGN KEY(Nomor_OA, BarangCode) REFERENCES TDPOJUAL2012(Nomor_OA, BarangCode)

Exception :

There are no primary or candidate keys in the referenced table 'TDPOJUAL2012' that match the referencing column list in the foreign key 'FK_TDPOJUAL2012_TDPOJUALBRGCUSTOMER2012'. Could not create constraint. See previous errors.

Thank you.
emi_sastra

ASKER
I get it.

Thank you very much for your help.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck