Solved

MULTI COLUMNS FOREIGN KEY

Posted on 2012-03-09
4
267 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now