2

everybody, I am relatively new to SQL and I am currently testing my database tables using Oracle Live SQL. I have a table called Customer and a table called Contact. Within the Contact table, I am trying to add a FOREIGN KEY constraint of the Customer_ID column into my Contact table, but keep getting an ORA-00904: "CUSTOMER_ID": invalid identifier, error using the code below:

ALTER TABLE Contact ADD FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID)

Any help would be much appreciated.

4
  • 1
    Do you have a Customer_ID field in your Contact table? Commented Mar 18, 2018 at 14:45
  • No I have a Customer_ID column in my Customer Table Commented Mar 18, 2018 at 14:48
  • and in Contact id which column name you have? .add your schema .. + Commented Mar 18, 2018 at 14:53
  • Please show the ddl for your table(s) and describe the relationship you're trying to codify. Commented Mar 18, 2018 at 15:00

2 Answers 2

2

Presumably, you don't have the column Customer_Id in contact. So try this:

ALTER TABLE Contact ADD Customer_Id number;  -- the type is a guess

ALTER TABLE Contact ADD FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID);
Sign up to request clarification or add additional context in comments.

1 Comment

WOW I cannot believe I forgot to even add the column Customer_ID into the Contact table. Thank you very much this solved the issue.
1

So based on the comments on your question, you don't have a Customer_ID column on your Contact table. The definition of a foreign key is that you have the column you want to reference in both tables.

ALTER TABLE Contact ADD Customer_ID int;
ALTER TABLE Contact ADD FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID);

1 Comment

Thanks for the response. You're correct, I forgot to add a column name in the Contact table called Customer_ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.