To: Nabil Khan
On Mon, 4 Oct 1999
Subject: Syntax to Make Foreign Key
> Dear Sir,
> I am a student who is looking for a help. My question is:
> I have two tables :
> ORDERS PRODUCTS
> ------ ---------
> ORDER_NUM (PRIMARY KEY) MFR_ID (PRIMARY KEY)
> MFR PRODUCT_ID(PRIMARY KEY
> PRODUCT ......
> .......
> The products table have composite primary key which is mfr_id and
> product_id.
>
> I did the following statement to add a foreign key to the orders table :
> ALTER TABLE ORDERS
> ADD FOREIGN KEY (MFR, PRODUCT) REFERENCES PRODUCTS;
>
> It did not let me do that and told me that parents key did not found.
> How do I add a foreign key to orders table to make it reference product
> table putting in mind that mfr and mfr_id have the same datatype and product
> and product_id have the same datatype. I dont know what I did wrong but it
> wont let me do that. I am trying to connect these two table together. How do
> I make a relation ship between two tables.
> Your help is appreciated .
>
> Sincerely,
>
> Nabil Khan
>
Nabil,
Your syntax was very close, but you forgot to specify which columns in the
PRODUCT table to use. The correct syntax is:
ALTER TABLE ORDERS ADD (CONSTRAINT fk_ORDERS1
FOREIGN KEY (MFR, PRODUCT)
REFERENCES PRODUCTS (MFR_ID, PRODUCT_ID));
Best regards,
-Ari Kaplan
Independent Oracle DBA Consultant
<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->
<-> For 335+ Oracle tips, visit my Web Page: <->
<-> <->
<-> http://www.arikaplan.com <->
<-> <->
<-> email: akaplan@interaccess.com <->
<-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><-><->
Back to Ari Kaplan's Home Page