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?
The 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.
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.