-1

How to delete a row (column) which have a dependency in another table in postgresql ??

suppose one table name is student and another one is transaction both tables having patron_id column, In that I am trying to delete a row in patron table of patron_id column,but it shows having dependency in transaction table so it shows cant update,

Even I am trying to delete that row in transaction again it shows same having dependency in patron table again.

Please tell me the solution how to delete rows if having dependencies?

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • 2
    come on, its not anything that you cannot find in 1min typing the title in google – mikus Oct 30 '15 at 11:31
  • Possible duplicate of [Delete data from dependent tables](http://stackoverflow.com/questions/3750758/delete-data-from-dependent-tables) – Daksh B Oct 30 '15 at 12:39
  • @mikus typing the title in google led me here, to find your lovely comment. And Daksh B, the possible duplicate you mention pertains to SQL Server 2008, not Postgres. – mitchus Aug 31 '18 at 12:54

1 Answers1

1

Use ON DELETE CASCADE foreign key definitions.

Alternately, you can delete the depending rows before the depended-on one, e.g.

begin;

delete from transaction
using patron
where transaction.patron_id = patron.id
  and patron.id = 'the_patron_id_to_delete';

delete from patron
where patron.id = 'the_patron_id_to_delete';

commit;
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778