Unable to delete a row from a table due to a dependency/reference in another table - sql

I have a stored procedure that deletes a record from a table where one of the columns matches a specified value:
ALTER PROCEDURE [dbo].[Sp_DelBranch] #datafield varchar(50)
AS
BEGIN
DELETE FROM Branch WHERE branchname = #datafield
END
Unfortunately I get the following error on execution:
The DELETE statement conflicted with the REFERENCE constraint "fk_BranchIdDept". The conflict occurred in database "MproWorkSpace", table "dbo.Department", column 'BranchId'.
Can anyone explain why I'm seeing this error?

conflicted with the REFERENCE constraint "fk_BranchIdDept".
This means, that the value you are trying to delete, is primary key in some other table and is referenced in this table as foreign key,i.e., a primary-foreign key relation is mapped through constraint fk_BranchIdDept ...So unless you delete the referred primary key, foreign key can not be removed from table.
Otherwise, this will lead to data inconsistency!
Look for Cascade Delete to help you in this!
SQL ON DELETE CASCADE, Which Way Does the Deletion Occur?

The DELETE statement conflicted with the REFERENCE constraint "fk_BranchIdDept".
The record you're trying to delete is referenced in other tables; deleting the Branch record would leave the other tables orphaned, and referencing something that no longer exists.
There are two approaches to this:
Delete all records in referenced tables before deleting from the Branch table
Perform a cascade delete (deleting from the Branch table triggers a delete in every referenced table)

The error means that there are some rows in Department table referencing the branch you're trying to delete.
You should either delete the corresponding rows from the Department table first, or change values in BranchId column for these rows, so they point to some other branch.

Related

Two FKs pointing to same parent column - ON UPDATE CASCADE - SQL Server

Here's the scenario.
- Parent Table: TEAMMEMBERS, with a primary key RecID
- Child Table: TEAMMEMBERTASKS
I have two columns in the TEAMMEMBERTASKS table, ReportedBy and AssignedTo.
Both of these columns use the RecID to store which team member reported a task and which team member the task is assigned to. The RecID could be the same for both columns, but that is not always the case.
I need to add in a FK for both child columns that check the relationship to the parent, and I would like to add ON UPDATE CASCADE to both of the foreign keys.
Whenever I try to do this, my second foreign key throws a 'may cause cycles or multiple cascade paths' error.
Here's my code:
ALTER TABLE [dbo].[TEAMMEMBERTASKS] WITH CHECK ADD CONSTRAINT
[FK_AssignedTo_TeamMemberRecID] FOREIGN KEY([AssignedTo])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT
[FK_AssignedTo_TeamMemberRecID]
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] WITH CHECK ADD CONSTRAINT
[FK_ReportedBy_TeamMemberRecID] FOREIGN KEY([ReportedBy])
REFERENCES [dbo].[TEAMMEMBERS] ([RecID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[TEAMMEMBERTASKS] CHECK CONSTRAINT
[FK_ReportedBy_TeamMemberRecID]
GO
With the current code, will this cause the RecID to be updated in both child columns or will it cause the update command to be restricted?
Should I just go ahead and write up a trigger that deals with this instead?

How can this SQL statement throw the following exception?

how can this statement:
DELETE FROM passage
WHERE passageid NOT IN
(
SELECT passageid from PreEndedPassages_passages
UNION SELECT fromPassageid from severalvisit
UNION SELECT toPassageid from severalvisit
UNION SELECT registerPassageid from stationobjects WHERE registerpassageid IS NOT NULL
UNION SELECT beginPassageid from stationobjects WHERE beginPassageid IS NOT NULL
UNION SELECT endPassageid from stationobjects WHERE endPassageid IS NOT NULL
)
throw this exception?
The DELETE statement conflicted with the FOREIGN KEY constraint "FK_statobj_begpasid". The conflict occurred in database "db.mdf", table "dbo.stationobjects", column 'beginpassageid'.
I have no clue, but it happened. beginPassageId is a foreign key on passageid.
EDIT:
Consider the NOT IN. I want to delete all passages that don't exist in one of its related tables. It usually works, but it happened once.
Thank you.
It means passage is parent table. and stationobjects is child table. You are trying to remove passageid from passage table which is present in stationobjects table as well.
First try to remove those passageid from stationobjects and then you can run this delete statement.
Alternate approach is cascade delete, if your DB supports that.
this kind of occur when you have a foreign key relationship in the two table.
This violates the Refrential Integrity .
so if you delete record from the primary table and record exist in foreign key table,
then you have two options:
1. either set the delete rule to cascade so that when ever you delete primary record the foreign key table record will get automatically deleted.
2.delete record from foreign key table first then from primary key table.
These kind of errors come up when table A foreign key is table B primary key and when you try to delete record in table A that has linking in table B, then deletion will not happen. Try dropping foreign key relation before delete. Same way truncate is used in tables by dropping fk relations and rebuilding them again after the table is reset

Conflict of the DELETE statement with the restriction REFERENCE

When I try to excecute this code, I'm getting error:
Conflict of the DELETE statement with the restriction REFERENCE "FK_Options_users". The conflict occurred in database "WINTOUR", table "PrintForm.Options", column 'user_code'
Can't understand why and how to fix that.
declare
#USER_CODE int;
select
#USER_CODE = 24;
delete from Settings.Items where user_code = #USER_CODE
delete from usnet where code = #USER_CODE
delete from usgroups where usercode = #USER_CODE
delete from users where code = #USER_CODE
It seems like Foreign Key constraint exists between the user_code column in PrintForm.Options and the code/user_code column in given tables.
If you try to delete all the data in given tables an error will occur as the user_code column in PrintForm.Options reference the data in the any one of the table from which yor are deleting the data.
To resolve the issue you should either drop and recreate the constraint FK_Options_users or delete the data from child table PrintForm.Options that the Foreign Key references ie where user_code = 24.
It looks to me like you are removing user 24, but the PrintForm.Options table has an entry that is still using it, and were it to be deleted, the foreign key would no longer be satisifed.
Have you perchance missed "Printform.Options" from the list of delete queries?
You have a foreign key relation with one of the rows you are trying to delete. That means that the key is used in another table. You must delete in the correct order so that does not happen.
You are missing a delete for the elements in specified in the error. So in Database WINTOUR in the table PrintForm.Options the use_code is a foreign key to the usercode you are deleting.
so you need to add
delete from PrintForm.Options where user_code = #USER_CODE
probably right before or after Settings.Items.

Error Adding Multiple FK Relationship in SQL Server 2008

Consider we have two tables ProductType and ProductSizeGroup as below
ProductType
Id
Name
MaleSizeGroupId
FemaleSizeGroupId
ChildSizeGroupId
ProductSizeGroup
Id
Name
Each of MaleSizeGroupId, FemaleSizeGroupId and ChildSizeGroupId fields should be FKs to ProductSizeGroup.Id.
I add one using the following statement:
ALTER TABLE [dbo].[ProductType]
WITH CHECK ADD CONSTRAINT
[FK_ProductType_ProductSizeGroup_Male] FOREIGN KEY([MaleGroupId])
REFERENCES [dbo].[ProductSizeGroup] ([Id])
This works fine. I try to add the next using
ALTER TABLE [dbo].[ProductType]
WITH CHECK ADD CONSTRAINT
[FK_ProductType_ProductSizeGroup_Female] FOREIGN KEY([FemaleGroupId])
REFERENCES [dbo].[ProductSizeGroup] ([Id])
But I get the error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_ProductType_ProductSizeGroup_Female". The conflict
occurred in database "dbname", table "dbo.ProductSizeGroup", column
'Id'.
So there is conflict.. but what conflict? What should I be looking for?
That just means: there are rows in your table ProductType that have values in the FemaleGroupId column which do not exist in the referenced table (ProductSizeGroup).
It's not a problem per se - you can totally have multiple columns going from one table to another.
The problem is with the existing data - you have data in there that doesn't live up to that FK constraint. Fix that data and you should be fine.
To find those offending rows, use a query like this:
SELECT *
FROM [dbo].[ProductType]
WHERE FemaleGroupId NOT IN (SELECT DISTINCT Id FROM [dbo].[ProductSizeGroup])
That will list all offending rows - update their attribute and get going again!

How to update 2 columns in 2 tables that have foreign key

I know the question of how to update multiple tables in SQL has been asked before and the common answer seems to be do them separately in a transaction.
However, the 2 columns I need to update have a foreign key so cannot be updated separately.
e.g.
Table1.a is a foreign key to Table2.a
One of the entries in the tables is wrong, e.g. both columns are 'xxx' and should be 'yyy'
How do I update Table1.a and Table2.a to be 'yyy'?
I know I could temp remove the key and replace but surely there's another way.
Thanks
You can't do the update simultaneously, however you can force SQL to do the update. You need to make sure your foreign keys have the referential triggered action ON UPDATE CASCADE
e.g.
ALTER TABLE YourTable
ADD CONSTRAINT FK_YourForeignKey
FOREIGN KEY (YourForeignKeyColumn)
REFERENCES YourPrimaryTable (YourPrimaryKeyColumn) ON UPDATE CASCADE
Not being a fan of on update cascade, I would suggest a different route.
First you do not update the Parent table, you add a new record with the value you want (and the same data as the other record for all other fields). Then you have no difficulty updating the child tables to use this value instead of that value. Further you now have the ability to to do the work in batches to avoid locking the system up while the change promulgates through it. Once all the child tables have been updated, you can delete the original bad record.
my answer is based on the following link: http://msdn.microsoft.com/en-us/library/ms174123%28v=SQL.90%29.aspx
You need to make sure that your table_constraint will be defined as ON UPDATE CASCADE
CREATE TABLE works_on1
(emp_no INTEGER NOT NULL,
project_no CHAR(4) NOT NULL,
job CHAR (15) NULL,
enter_date DATETIME NULL,
CONSTRAINT prim_works1 PRIMARY KEY(emp_no, project_no),
CONSTRAINT foreign1_works1 FOREIGN KEY(emp_no) REFERENCES employee(emp_no) ON DELETE CASCADE,
CONSTRAINT foreign2_works1 FOREIGN KEY(project_no) REFERENCES project(project_no) ON UPDATE CASCADE)
and then when you will change the value of your primary key
see the following quote:
For ON DELETE or ON UPDATE, if the CASCADE option is specified, the
row is updated in the referencing table if the corresponding
referenced row is updated in the parent table. If NO ACTION is
specified, SQL Server Compact Edition returns an error, and the update
action on the referenced row in the parent table is rolled back.
For example, you might have two tables, A and B, in a database. Table
A has a referential relationship with table B: the A.ItemID foreign
key references the B.ItemID primary key.
If an UPDATE statement is executed on a row in table B and an ON
UPDATE CASCADE action is specified for A.ItemID, SQL Server Compact
Edition checks for one or more dependent rows in table A. If any
exist, the dependent rows in table A are updated, as is the row
referenced in table B.
Alternatively, if NO ACTION is specified, SQL Server Compact Edition
returns an error and rolls back the update action on the referenced
row in table B when there is at least one row in table A that
references it.