Add Relationship - sql

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

Related

How to alter the table to add foreign key constraint referencing primary key of same table

When I try to alter the table to add the foreign constraint, it is throwing the below error
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_ManagerId". The conflict occurred in database "MX_STAG", table "dbo.Sales_US_User", column 'ID'.
I tried the below queries,none of them have worked.
ALTER TABLE SALES_US_USER
ADD CONSTRAINT FK_ManagerId
FOREIGN KEY (ManagerID) REFERENCES SALES_US_USER(id);
ALTER TABLE SALES_US_USER
ADD FOREIGN KEY (ManagerID) REFERENCES SALES_US_USER(id);
Please help
You've got the DDL right. That error is telling you that you have a row with a ManagerId that does not map to an Id. EG
use tempdb
go
drop table if exists sales_us_manager
go
create table sales_us_user(Id int primary key, ManagerId int)
insert into sales_us_user(id,managerId) values (1,2)
ALTER TABLE SALES_US_USER
ADD CONSTRAINT FK_ManagerId
FOREIGN KEY (ManagerID) REFERENCES SALES_US_USER(id);
--Msg 547, Level 16, State 0, Line 12
--The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_ManagerId". The conflict occurred in database "tempdb", table "dbo.sales_us_user", column 'Id'.
select *
from SALES_US_USER
where ManagerId not in
(select id from SALES_US_USER )
You need to delete both table data before updated database with your new migration. One of the common mistakes that you add a FK (foreign key) from table to another while any on these table contains data. The constraint happens because the FK can't be null and while you are adding the new migration the database find that the new column that will be created will be null so, delete the data in both tables first.
The issue got resolved after finding and updating the rows with an an invalid ManagerId, and then created the constraint.

cannot alter table to add a foreign key

alter table course_relation
add foreign key (offered_By)
references department_relation(dept_cd)
alter table department_relation
add foreign key(hod)
references staff_relation(staff_no)
alter table staff_relation
add foreign key (dept_cd)
references department_relation(dept_cd)
//the third para doesn't work but the first two works fine
error message:
Msg 547, Level 16, State 0, Line 10 The ALTER TABLE statement
conflicted with the FOREIGN KEY constraint
"FK__staff_rela__dept__1BC821DD". The conflict occurred in database
"Royal_Poly_DB", table "dbo.Department_Relation", column 'Dept_Cd'.
You can't create circular foreign key references.
In this Statement
alter table department_relation
add foreign key(hod)
references staff_relation(staff_no)
In this alter query you create foreign key reference in 'department_relation' from 'staff_relation' table.
alter table staff_relation
add foreign key (dept_cd)
references department_relation(dept_cd)
In this alter query you create foreign key reference in 'staff_relation' from 'department_relation' table.
This is circular dependency of foreign key and it is allowed.

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

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