Deleting a record in cascading tables

1

I have a table "Terms" that your ID is FK from "Expression Terms".

As shown in the figure below.

When I run the command "delete Terms where IDTermo = 4" sql generates me

the following error.

  

The DELETE statement conflicted with the REFERENCE constraint   "FK_TermoExpress_Termos1". The conflict occurred in database   "SprintJuncao", table "dbo.TermoExpressao", column 'IDTermoExpressao'.   The statement has been terminated.

From what I read it seems to me that this is a cascade exclusion, when I delete the "Terms" record I automatically delete the records related to "TermsExpression", In the case all the registrations in TermExpressao with IDTermo = 4

Can anyone help me how should I proceed in this case?

    
asked by anonymous 03.12.2014 / 14:18

1 answer

2

The best solution then is to change your FK so that it is delete cascade

ALTER TABLE [dbo].[TermoExpressao] DROP CONSTRAINT [FK_TermoExpressao_Termos1]
GO
ALTER TABLE [dbo].[TermoExpressao]  WITH CHECK ADD  CONSTRAINT [FK_TermoExpressao_Termos1] FOREIGN KEY([IDTermo])
REFERENCES [dbo].[Termo] ([IDTermo])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TermoExpressao] CHECK CONSTRAINT [FK_TermoExpressao_Termos1]
GO

Another option, to solve this would be to modify your FK according to the images.

    
03.12.2014 / 14:54