Delete rows in multiple tables

Asked

Viewed 204 times

-1

I’m trying to erase information from two tables at the same time that are connected to each other but I’m not succeeding, give me this error: Somebody please help me?

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    Your error speaks of constraints breach error, you can disable them before deleting the records.

  • -1 For the code and information that are attached in images.

  • What do you mean? Could you please be more explicit?

  • Miguel, welcome. The ideal is that you report the error and the code in the question itself making use of formatting. Thus avoid the use of the image. See more here: http://answall.com/help/asking

  • Which database? Do you want to delete all data from this table that has relation to another? or delete the data of the two tables that have the same relation?

1 answer

1

As you did not specify the database, I am responding in SQL SERVER, and tables from my database;

You can take advantage of the "deleted id" table something like:

begin transaction;

   declare @deletedIds table ( id int );

   delete e
   output p.IdPessoa into @deletedIds
   from Pessoas p
    join Estados e
    on e.IdPessoa = p.IdPessoa

   delete P
   from Pessoas P
    join @deletedIds d
      on d.id = P.IdPessoa;

commit transaction;

You can take the table id with the "output" note that the table that is the first to be deleted is the one that has dependency relation in this case (States).

  • It’s in Java SQL, can you help me in Javasql? thanks for the help @Marconcíliosouza

  • Java sql. Have a database of these not known

Browser other questions tagged

You are not signed in. Login or sign up in order to post.