Oracle SQL-ALTER TABLE Error - sql

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);

Related

How to add unique constraint as foreign key ?

I am trying to add unique constraint as foreign key by this statement:
ALTER TABLE SOME_TABLE ADD(
CONSTRAINT FK_ID FOREIGN KEY (S_ID) REFERENCES OTHER_TABLE(O_ID) UNIQUE (S_ID)
);
I thought that this statement is correct, but all time I got "missing right parenthesis error". Probably I have wrong order of key words.
Could you give me advice how to create an unique constraint ?
I red this issue:
Add a unique constraint of a sql table as foreign key reference to an another sql table
but still I have problem with this.
First, you don't need parentheses. Second, this is two constraints and you might as well give both names:
ALTER TABLE SOME_TABLE
ADD CONSTRAINT FK_ID FOREIGN KEY (S_ID) REFERENCES OTHER_TABLE(O_ID);
ALTER TABLE SOME_TABLE
ADD CONSTRAINT UNQ_ST_S_ID UNIQUE (S_ID);

Can't add foreign key to a table

I've checked that none of the two tables has any foreign keys. I've checked that they both have Id of type uniqueidentifier. I runt the script and get this eror.
ALTER TABLE [dbo].[Records]
ADD CONSTRAINT [FK_dbo.Records_dbo.Users_UserId]
FOREIGN KEY ([UserId])
REFERENCES [dbo].[Users] ([Id])
--ON DELETE CASCADE
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Records_dbo.Users_UserId". The conflict occurred in database "MyDb", table "dbo.Users", column 'Id'.
Not sure how to troubleshoot it... Tested both with and without the cascade...
It must be something to do with the existing data. There must be some records conflicting the foreign key creation. Try to create the key on an empty schema to see if it works. Use WITH NOCHECK to not to check existing rows at the time of foreign key creation, if you need that data.
Most likely you have a UserId in the Records table that does not exist in the Users table. This will certainly happen if you've been using 0 or some other value as a "default" value.

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);

Foreign Key invalid identifier

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);

Is there any way to make the following sql query work?

I am trying to create two tables using the following SQL:
create table student(sid char(20) primary key,name char(20),age int,hours char(10) references courses(cid));
create table courses(cid char(10),cname char(10),grader char(20) references student(sid));
However I get the following error:
1: ERROR: relation "courses" does not exist
3: ERROR: relation "student" does not exist
Is there any way or syntax which can solve this problem?
You would need to create the tables first (without REFERENCES clause). After that create your foreign keys manually by statement ALTER TABLE mytable ADD CONSTRAINT mytablefk FOREIGN KEY... But first I'd consider if there really is a relationship from table courses to table student!
Rather than creating the Foreign Key constraints at the same time as the tables with the References short-hand, you can add one or both of them afterwards with an Alter Table Add Constraint command. See the Alter Table page in the PostgrSQL manual here.
As mu pointed out, the target of a foreign key has to have a Unique or Primary Key constraint defined, so I've added that on the cid column in the example below.
In your case, it could look something like this:
create table student(sid char(20) primary key,name char(20),age int,hours char(10));
create table courses(cid char(10) primary key,cname char(10),grader char(20));
Alter Table student Add Constraint fk_student_hours_cid Foreign Key (hours) References courses(cid);
Alter Table courses Add Constraint fk_courses_grader_sid Foreign Key (grader) References student(sid);