Error while adding a foreign key constraint - sql-server

Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"AB_TRANS_FK3". The conflict occurred in database "DEV", table
"dbo.PRODUCT", column 'ID'.
Hello all,
I need your help with this error. I am trying to add a foreign key constraint here using the following query
ALTER TABLE [dbo].[AB_TRANS] WITH CHECK ADD CONSTRAINT [AB_TRANS_FK3] FOREIGN KEY([ID])
REFERENCES [dbo].[PRODUCT] ([ID])

Check your table to see which row(s) are causing the issue, then you'll need to fix them. This should help:
SELECT *
FROM
dbo.AB_Trans A
WHERE
NOT EXISTS (SELECT * FROM dbo.Product P WHERE P.ID = A.ID)
If there isn't a match, are you using "0" for the ID or some other dummy value? This could certainly cause the problem as you should be using NULL in those cases.

Related

Deleted Foreign Key Constraint causing error

I'm trying to create a FK constraint by using this auto created script.
USE [PRM.Mobile.SaaS]
GO
ALTER TABLE [dbo].[Activity] WITH CHECK
ADD CONSTRAINT [FK_Activity_Activity]
FOREIGN KEY([ParentActivityID]) REFERENCES [dbo].[Activity] ([ActivityID])
GO
ALTER TABLE [dbo].[Activity] CHECK CONSTRAINT [FK_Activity_Activity]
GO
But I get these errors:
Msg 547, Level 16, State 0, Line 4
The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Activity_Activity". The conflict occurred in database "PRM.Mobile.SaaS", table "dbo.Activity", column 'ActivityID'.
Msg 4917, Level 16, State 0, Line 8
Constraint 'FK_Activity_Activity' does not exist.
Msg 4916, Level 16, State 0, Line 8
Could not enable or disable the constraint. See previous errors.
Looks pretty straightforward but I don't have any FK with a name of FK_Activity_Activity anymore. Also first message being a FK constraint preventing the action and second message being that constraint is not existing is a bit confusing.
I double checked the INFORMATION.SCHEMA and sys.objects and nothing with a name of FK_Activity_Activity shows up.
These are the queries I used for checking
SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME LIKE '%Activity_Activity%'
and
SELECT
OBJECT_NAME(object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
type_desc AS ConstraintType
FROM
sys.objects
WHERE
type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(parent_object_id) = 'Activity'
What am I missing here?
The add constraint failed with the first error: the FK is not created.
The second error is from the second statement because the FK does not exist.
Everything else follows from there.
R TABLE [dbo].[Activity] WITH CHECK
ADD CONSTRAINT [FK_Activity_Activity]
FOREIGN KEY([ParentActivityID]) REFERENCES [dbo].[Activity] ([ActivityID])
To fix things so you can create the index you need to correct your data so it is consistent with the new FK before you create the index. Whether than is modifying the data in ParentActivityID or ActivityID only you can say.
But note any recursive structure needs to allow for ParentActivityID being null (unless the graph is specifically cyclic). Usually this is by making it null.

How do I get rid off error on adding Foreign Key to a table which contains data

I am using the below written query to add a foreign key to an Existing Field with data:
ALTER TABLE ServiceDetails
ADD CONSTRAINT fk_ServiceDetails
FOREIGN KEY (OCF_ID)
REFERENCES OCF_Commerce(OCF_Internal_ID);
But I am getting an error after executing the query. What am I missing?
The error message is:
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "fk_ServiceDetails". The conflict occurred in database "TrulyDB", table "dbo.OCF_Commerce", column 'OCF_Internal_ID'.
You probably have inconsistent data in your tables
try running
select *
from ServiceDetails
where OCF_ID not in (select OCF_Internal_ID from OCF_Commerce)
This will output all rows with ID absent in master table

sql - update two key columns without conflict

I want to to update a column from table patient, and add a prefix before all its fields, like
update patient
set contactdetailsID = '99999'+ contactdetailsID
where patient.id = 5294
So, row with id = 5294, will have the same value in column contactdetailsID with prefix 99999.
Before | After
012345 99999012345
the issue i am facing is that patient.contactdetailsID is the foreign key for another table "contactdetails".
So i get the following error.
Msg 547, Level 16, State 0, Line 1 UPDATE statement conflicted with
COLUMN FOREIGN KEY constraint 'ContactDetails_Patient_FK1'. The
conflict occurred in database 'A', table 'ContactDetails',
column 'id'. The statement has been terminated.
(0 row(s) affected)
How can i make that change? I want to change both patient.contactdetailsID and its key Contactdetails.ID, with the same prefix in order not to lose the connection between them.
You can temporary disable the Foreign-Key Constraint using:
ALTER TABLE *Tablename*
NOCHECK CONSTRAINT *ForeinKey_Name*;
and update both the ContactDetails Table and Patient Table.
But make sure that you enable the ForeignKey afterwards!
To enable a Foreign Key use
ALTER TABLE *Tablename*
CHECK CONSTRAINT *ForeinKey_Name*;
You could implement ON UPDATE CASCADE in your table.
see MySQL documentation

Unable to get a Foreign key

USE Kudler_FF
ALTER TABLE Employee_Tbl
ADD CONSTRAINT FK_Employee_Tbl
FOREIGN KEY (JobTitle) REFERENCES Job_Tbl (JobTitle);
Message I am getting:
Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Employee_Tbl". The
conflict occurred in database "Kudler_FF", table "dbo.Job_Tbl", column
'JobTitle'.
What did I add or not add
You usually only get an error adding a foreign key constraint when the constraint would be violated by the current data.
In other words, you probably have a value in Employee_Tbl(JobTitle) that does not exist in Job_Tbl(JobTitle).
You would not be able to add such a constraint until your data is modified so that a violation would not occur.
Find the values for JobTitle in Employee_Tbl that don't exist in Job_Tbl and then add them to that latter table.
I'm not sure of the exact syntax for SQL Server but you could start with:
select distinct JobTitle from Employee_Tbl
where JobTitle not in (
select distinct JobTitle from Job_Tbl
)

SQL Constraint-Insert Fails

I have two table table1 and table2. I have a record in table1. I want to insert record into table2. but it comes up with the following Exception.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Table2_<Column3>". The conflict occurred in database "<databaseName>", table "table1", column 'Id'.
The constraint on the table2 is like this.
ALTER TABLE [dbo].[table2] ADD CONSTRAINT [DF__table2__name__0B91BA14] DEFAULT ((0)) FOR [column4]
The error shows that you also have a foreign key constraint and you are trying to insert a row into a child table that doesn't have corresponding record in master
The exception message is telling you which constraint is causing the issue. It's against Column3 (at least that's what the name suggests) and is a foreign key constraint against table1.
You're not showing any sample SQL but it would appear you're trying to insert data in to table2 where the foreign key value specified for Column3 does not exist in table1.
Your Comment helped me.
I have used this Query to insert data into the new database from the old databse.
INSERT INTO Database.[dbo].[Table2] ([colmn1]
,[colmn2]
,[colmn3]
,[columns4]
)
SELECT [colmn1]
,[colmn2]
,[colmn3]
,[columns4]
FROM [OtherDatabase].[dbo].[Table2]
But there is a constarint on colum3 which is foreign key on Table1.
I have changed the colmn3 of table1 to same Guid as otherDatabase Guid and it works.
Thanks All

Resources