Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MULTI COLUMNS FOREIGN KEY

Posted on 2012-03-09
4
270 Views
Last Modified: 2012-03-10
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.
0
Comment
Question by:emi_sastra
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 37704788
ALTER TABLE tableb
ADD CONSTRAINT FK_tableb_tablea FOREIGN KEY (PurchaseOrderNo, ItemNo_A)
    REFERENCES tableA (PurchaseOrderNo, ItemNo_A) ;
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 37704833
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.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 37704840
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.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 37704867
I get it.

Thank you very much for your help.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Merige returns error code when updating 15 54
T-SQL to Update Table Dynamically 2 55
Strange msg in the SSMS pane 13 58
How can i use WITH CTE for checking exist value? 3 45
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question