This question already has an answer here:
FOREIGN KEY SAME TABLE error - but foreign key doesn't exist
(1 answer)
Closed 6 years ago.
I like to know if it's possible to create a self refering constraint using an ALTER TABLE statement. (tsql SQL Server 2012)
expected I have a table with just a Primary Key called ID and column called parent_id
I want to do something like this:
ALTER TABLE myTable
ADD CONSTRAINT FK_myTablemyTable
FOREIGN KEY (parent_Id)
REFERENCES myTable(Id)
but I just get the error:
The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint
Is there an alternative way to get the statement working, or do you have to recreate the whole table in this case ?
Hope somebody is able to help
Cheers
Problem solved
I am creating some kind of table migration tool and have not realized that the data was inserted already ...
Problem was that the root element was Inserted with parent_id = 0 and not null...
So there is no mistake in the SQL statement.
now it's working
Related
I am trying to migrate a table which has 2 colums.
Id - Primarky key
Parent ID - Foreign key populated by the value above (Id).
(So basically the FK is in the same table)
When I migrate this, I get the following error.
"The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint"
Please let me know how to deal with this.
Thanks in advance
Disable the foreign key constraint first. Then migrate your data. And afterwards you can enable the constraint again.
ALTER TABLE Purchasing.PurchaseOrderHeader
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
ALTER TABLE Purchasing.PurchaseOrderHeader
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
Disable foreign key constraints with INSERT and UPDATE statements
This question already has answers here:
The INSERT statement conflicted with the FOREIGN KEY constraint
(3 answers)
Closed 6 years ago.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__faredestin__svno__503BEA1C". The conflict occurred in database
"travelproject", table "dbo.bus", column 'svno'. The statement has
been terminated.
svno column is used as FOREIGN KEY in table.you have to consider FOREIGN KEY hierarchy while insert/delete operation.And FOREIGN KEY must be unique value and record must be in main table for given FOREIGN KEY.
while Inserting in to table check what value goes in svno column and check whether their is record for given svno in table where svno is used as a PRIMERY KEY.
I'm trying to update a value (small spelling error) in the SQL Server 2014 database.
I do like this:
update t_Table set funID = 'References' where funID = 'Referencies'
When doing this I get the error
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_t_Table_Language_t_Table". The conflict occurred in database "db", table "dbo.t_Table".
If updating the foreign key, I get a similar error.
Is there any way to update all values at the same time?
I have a vague memory of someone showing a way to do this in management studio but I do not recall how.
Enable update cascade on the foreign key
ALTER TABLE t_Table_Language DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table_Language ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table(funID)
ON UPDATE CASCADE
EDIT:
Or the other way around, i'm not sure which table has the foreing key
ALTER TABLE t_Table DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table_Language(funID)
ON UPDATE CASCADE
Then do the update on the referenced table (the master table).
UPDATE t_Table_Language
SET funID = 'References'
WHERE funID = 'Referencies'
This question already has answers here:
Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?
(29 answers)
Closed 8 years ago.
I am trying to TRUNCATE a table but I am getting this error:
Cannot truncate a table 'ALOT_OF_USELESS_LOGS' because it is being referenced by a FOREIGN KEY constraint.
However, I have already truncated all the table where it is referenced EXEC sp_fkeys 'ALOT_OF_USELESS_LOGS' and they all truncated successfully but over this table I am still getting this error.
How can I let this go, ideally without dropping constraints?
Restrictions of Truncate (source: MSDN)
You cannot use TRUNCATE TABLE on tables that:
Are referenced by a FOREIGN KEY constraint. (You can truncate a
table that has a foreign key that references itself.)
Participate in an indexed view.
Are published by using transactional replication or merge
replication.
So you cannot use truncate command over a table if referenced by a Foreign Key
Write down
show create table TABLENAME
This will show you all the table schema, check the constraint that you have created, remove it and delete it.
This question already has answers here:
SQL Server add auto increment primary key to existing table
(16 answers)
Closed 8 years ago.
How to alter column in the existing table for the primary key and identity. I tried below query but showing Incorrect syntax near the keyword 'identity'.
alter table IAM_Software_Licence_Master
alter column SoftwareLicId int identity(1,1) primary key
How can I achieve this..
Its difficult to say what the issues are with changing this on your end as I do not know the table structure or whether or not this is used as a foreign Key. But the below link should give you everything you need:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032/how-to-alter-column-to-identity11