Adding FOREIGN KEY Constraint in column - sql

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)

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

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

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

What is the Difference between adding Column as Foreign Key and as a Constraint

I am Using Oracle 10g. I am Adding new column deptId to my UserList Table where I use deptId column as Foreign key which references other table Column Departments.DepartmentId
Is there Difference between adding foreign key as constraint and First Query
Query1
ALTER TABLE UserList
ADD FOREIGN KEY (DeptId)
REFERENCES Departments(DepartmentId)
Query2
ALTER TABLE UserList
ADD CONSTRAINT fk_DeptId FOREIGN KEY (DeptId)
REFERENCES Departments(DepartmentId)
There is no difference except in your use of the optional "CONSTRAINT" and constraint name clause.
There are two kinds of constraint definition: inline and out of line. The former operates on a column as part of the column definition, and hence does not need to name the DeptID column. The latter is part of the table definition and therefore does.
Both of your examples are out of line constraints, but you have not named the constraint in the former case, which is a bad practice:
http://docs.oracle.com/cd/E18283_01/server.112/e17118/clauses002.htm#g1053592
The second syntax allows you to name your constraint. The first doesn't.
ALTER TABLE [dbo].[UserList] WITH NOCHECK ADD CONSTRAINT [fk_DeptId] FOREIGN KEY([DeptId])
REFERENCES [dbo].[Departments] ([DepartmentId])
No, there is no difference in both of your queries.But you to name the foreign key constraints names . You can use the above query.