Foreign Key invalid identifier - sql

I am trying to add a foreign key to my table but i am getting this error,
ERROR at line 3: ORA-00904: "DEDUCID": invalid identifier
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc;
I have this other table named pr_deduc that has a column named deducid, that is a char with one value as my primary key. I have it spelled corrected, unless i am missing something.

The deducid you mention has to be a column on pr_cust, and you are not referencing the column in the other table. The propper syntax is:
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);

ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);

Related

Why getting error: No matching unique or primary key for this column-list, if I have the same columns?

I need to add a Foreign key to my Date_Reserved attribute in the Additional_extra Table, (Maybe because the Date type can't be unique?
I keep getting the next error:
Error report - ORA-02270: no matching
unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
For The next Code:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
This is the part of the task that I need to create. So it is given that Date_Reserved must be FK:
Any Suggestions on what is wrong?
I tried to add FK when creating the Table, however when I find out that just the Date_Reserved FK line is wrong I created without that, but the error is still the same.
I tried from the GUI to add a Foreign key but same error
date_reserved is part of a composite primary key consisting of many columns; there is no unique or primary key that is solely on the date_reserved column.
Your constraint:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Date_Reserved)
REFERENCES Reservation(Date_Reserved)
);
Is trying to refer to a unique constraint that is solely on the date_reserved column and that does not exist so the SQL engine (correctly) raises the exception that such a constraint does not exist.
What you need to do is refer to the entire composite key:
ALTER TABLE Additional_Extra
Add(
CONSTRAINT test_date
FOREIGN KEY(Booking_Number, Room_Number, Date_Reserved)
REFERENCES Reservation(Booking_Number, Room_Number, Date_Reserved)
);
fiddle

Adding FOREIGN KEY Constraint in column

How can i add FOREIGN KEY constraint in the referrer_id(same as profile id) column? Since the column name contains parentheses it is throwing error
ALTER table Referrals ADD Constraint fk_referrer_ID
FOREIGN KEY(referrer_id(same as profile id)) REFERENCES Profiles(profile_id)
ERROR: Incorrect syntax near 'referrer_id(same as profile id)'.
Use below query, column name delimited by brackets ([ ]):
ALTER table Referrals ADD Constraint fk_referrer_ID FOREIGN KEY([referrer_id(same as profile id)]) REFERENCES Profiles(profile_id)
I believe referrer_id is your column name
ALTER table Referrals ADD Constraint fk_referrer_ID
FOREIGN KEY(referrer_id) REFERENCES Profiles(profile_id)
You don't to specify that it is same as profile id in foreign key, REFERENCES Profiles(profile_id) conveys that message to compiler.
or if you really have a ugly column name(referrer_id(same as profile id)) then you need to use square brackets to escape the special characters present in your column name
ALTER table Referrals ADD Constraint fk_referrer_ID
FOREIGN KEY([referrer_id(same as profile id)]) REFERENCES Profiles(profile_id)

Oracle SQL-ALTER TABLE Error

I've been looking over the following SQL code for awhile and just can't seem to find the problem. I'm relatively new to SQL, so I'm sure it's just something I'm overlooking. The error message I get is: ORA-01735: Invalid ALTER TABLE option.
Code:
ALTER TABLE PATIENT
(
ADD CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
ADD CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL
);
I have triple checked to make sure the foreign key column names and the referenced column names are correct.
seems The parentheses are in wrong place
ALTER TABLE PATIENT
ADD (CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL);

Add Relationship

I am trying to add a foreign key into an existing table, this is what I have so far:
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT Invoice_ItemFK FOREIGN KEY (ProdID);
I get this error:
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
Any clue how to circumvent this?
You need a REFERENCES clause to list which table you want the foreign key to point to. For example, assuming ProdID is the primary key of the PRODUCTS table:
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT Invoice_ItemFK
FOREIGN KEY (ProdID)
REFERENCES PRODUCTS (ProdID);
You need to specify the REFERENCES clause. I have guessed at reference here - but you get the idea. The actual issue was the name of the Foreign Key. It should start with FK_.
ALTER TABLE INVOICE_ITEM
ADD CONSTRAINT FK_Invoice_Item
FOREIGN KEY (ProdID)
REFERENCES Prod(ProdID);

Can't add foreign keys because unique constraint needed

I'm brand new to using Oracle SQLDeveloper and I'm working on a college project right now. I keep trying to add foreign key constraints to my tables(which already hold the foreign key as an attribute) so Im using ALTER like this:
alter table applies
add constraint e_number foreign key (e_number)
references student (e_number);
where e_number is the primary key in a table called student. The student table's e_number has the primary key constraint and also has an index that was auto-generated where it says UNIQUE under the UNIQUENESS column in the indexes tab. Whenever I try and create a foreign key for any of my tables I'm getting this same error everytime:
Error starting at line : 1 in command -
alter table applies
add constraint e_number foreign key (e_number)
references student (e_number)
Error report -
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
*Cause: The specified constraint name has to be unique.
*Action: Specify a unique constraint name for the constraint.
I'm a bit confused and have been reading about unique on several sites but still don't get it. When I call an ALTER I can either specify a FOREIGN key or specify a UNIQUE key. Am I supposed to ALTER unique and then ALTER foreign? What am I doing wrong?
It's because you already have a key named e_number. Try:
alter table applies
add constraint applies_student_e_number foreign key (e_number)
references student (e_number);