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

Recommended Answers

All 3 Replies

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.

How To cascade I don't know how to do that

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.