I want to have a FK constraint in a child table so that when I delete a record from the child table, the primary record is NOT deleted.
I did create a FK constraint in the child table with ON DELETE CASCADE
thinking that this would delete only the child-table record when a child table delete record operation is intended and when deleting the primary table record.
I know that cascade delete operation will/should cascade to the child-table.
ALTER TABLE [dbo].[PTINSPECTION] WITH CHECK
ADD CONSTRAINT [FK_PTINSPECTION_MOTRIP]
FOREIGN KEY([UID_MOTRIP])
REFERENCES [dbo].[MOTRIP] ([UID_MOTRIP])
ON DELETE CASCADE
This may sound stupid, but I googled questions about cascade delete and a couple of responses stated that cascade delete would apply ALSO to the primary table (meaning it would delete the corresponding parent record).
This did not make sense to me and this is why I am asking this question...
Is it possible that the child FK constraint allows for deleting the primary record when a deleting a child table record or records?
I want to have a FK constraint in a child table so that when I delete a record from the child table, the primary record is NOT deleted.
I did create a FK constraint in the child table with ON DELETE CASCADE
thinking that this would delete only the child-table record when a child table delete record operation is intended and when deleting the primary table record.
I know that cascade delete operation will/should cascade to the child-table.
ALTER TABLE [dbo].[PTINSPECTION] WITH CHECK
ADD CONSTRAINT [FK_PTINSPECTION_MOTRIP]
FOREIGN KEY([UID_MOTRIP])
REFERENCES [dbo].[MOTRIP] ([UID_MOTRIP])
ON DELETE CASCADE
This may sound stupid, but I googled questions about cascade delete and a couple of responses stated that cascade delete would apply ALSO to the primary table (meaning it would delete the corresponding parent record).
This did not make sense to me and this is why I am asking this question...
Is it possible that the child FK constraint allows for deleting the primary record when a deleting a child table record or records?
Share Improve this question edited Feb 6 at 20:07 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 6 at 20:01 John DJohn D 6872 gold badges10 silver badges27 bronze badges 15 | Show 10 more comments1 Answer
Reset to default 1The font of all knowledge is the manual - in the create table section the pertinent details for FKs are
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }
Specifies what action happens to rows in the table created, if those rows have a referential relationship and the referenced row is deleted from the parent table.
CASCADE
Corresponding rows are deleted from the referencing table if that row is deleted from the parent table.
enter link description here
A consequence of this is (as others have pointed out) you may end up in the situation where parents/referenced row has no children/referencing rows. SQLSERVER does not have inbuilt utility to deal with this - but that's a different question.
ON DELETE CASCADE
? You are telling it to also delete the parent. – Eric Commented Feb 6 at 20:06ON DELETE
clause. Just to make sure it's all well understood. – The Impaler Commented Feb 6 at 22:17