In my database there two table call sample and sample customer in sample table primary key is sample_Note_no and it is a foreign key in sample_customer table I got same error after I wrote this coding also

ALTER TABLE Sample Customer
ADD CONSTRAINT FK_Sample Customer_Sample
FOREIGN KEY (Sample_NOTE_No)
REFERENCES Sample(Sample_NOTE_No)
ON DELETE CASCADE
GO
Attachments Shehara.png 43.4 KB

Hi,

I think you are applying the cascade delete to the wrong table.

You are altering the table Sample Customer to include a Foreign Key (Sample_Note_No) linked to Sample.Sample_Note_No but you are then saying if the record in Sample Customer is deleted, delete the corresponding record in Sample.

Therefore SQL is stopping you because if you did this you would delete the record in Sample and potentially leave orphaned records in the Sample Customer table (assuming a One to Many relationship, which SQL server is.)

I think you want to apply the cascade to the Sample Table so if you delete the record there you delete any child records in Sample Customers.

You added "ON DELETE CASCADE" to your statement. You should remove this from the Customer_Sample table and apply it to the Sample table.

This question has already been answered. Start a new discussion instead.