dbTalk Databases Forums  

[SQL] Deleting entries from multiple tables

mailing.database.pgsql-sql mailing.database.pgsql-sql


Discuss [SQL] Deleting entries from multiple tables in the mailing.database.pgsql-sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
manidegr8
 
Posts: n/a

Default [SQL] Deleting entries from multiple tables - 11-29-2010 , 11:59 PM






i am trying to run a query but its not working may be due to constraint
conflicts

i have table A, B and C
B and C have a foreign key linked with A's primary key

so i want to delete an entry from A
for that i hav to delete child records first

can u design a query which wont conflict the constraints..?

Thanks...
Regards.
--
View this message in context: http://postgresql.1045698.n5.nabble....4p3285654.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
  #2  
Old   
Dusan Misic
 
Posts: n/a

Default Re: [SQL] Deleting entries from multiple tables - 11-30-2010 , 02:15 AM






What is ON DELETE part of the foreign key constraint?

If you set it to CASCADE, then your delete from the parent table will
cascade to child tables (to put it simpler, when you delete record in table
A, then PostgreSQL will delete any rows in tables B and C that are
referencing original row (or column) in table A).

If you used defaults when you created your FOREIGN KEY constraint, the
default is to restrict ANY deletion in table A if it is being referenced in
table B or C.

On Tue, Nov 30, 2010 at 6:59 AM, manidegr8 <engineer.usman (AT) ymail (DOT) com> wrote:

Quote:
i am trying to run a query but its not working may be due to constraint
conflicts

i have table A, B and C
B and C have a foreign key linked with A's primary key

so i want to delete an entry from A
for that i hav to delete child records first

can u design a query which wont conflict the constraints..?

Thanks...
Regards.
--
View this message in context:
http://postgresql.1045698.n5.nabble....4p3285654.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
  #3  
Old   
Dusan Misic
 
Posts: n/a

Default Re: [SQL] Deleting entries from multiple tables - 11-30-2010 , 02:19 AM



To be precise, if you used defaults, you can't delete any row in table A
that has rows referencing to it in tables B and C.

On Tue, Nov 30, 2010 at 6:59 AM, manidegr8 <engineer.usman (AT) ymail (DOT) com> wrote:

Quote:
i am trying to run a query but its not working may be due to constraint
conflicts

i have table A, B and C
B and C have a foreign key linked with A's primary key

so i want to delete an entry from A
for that i hav to delete child records first

can u design a query which wont conflict the constraints..?

Thanks...
Regards.
--
View this message in context:
http://postgresql.1045698.n5.nabble....4p3285654.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
  #4  
Old   
Achilleas Mantzios
 
Posts: n/a

Default Re: [SQL] Deleting entries from multiple tables - 11-30-2010 , 02:36 AM



Here is my take on how to regard parent tables in one-to-many FK relationships.
If lets say we have a situation when we model e.g. mail messages and its
attachments, then we might want to use ON DELETE CASCADE since
there is absolutely no reason for an attachment to exist when the main message is gone.
But, when we model e.g. machinery in a car and we have an attribute : makerof parts modeled
as a parent table and the child parts table pointing to this maker table,
then this parent table is not of high significance and we should not put ONDELETE CASCADE.
In this way, we protect the database of deleting vital data.

Note that under the parts table, other tables might point to, which would include history,
plan maintenace, and other very vital data.

So, you are enouraged to put ON DELETE CASCADE ONLY when you are confident that the child table's rows
have absolutely no meaning without the parent table row.
Otherwise, leave the default behaviour.

Στις Tuesday 30 November 2010 10:19:04 ο/η Dusan Misic *γραψε:
Quote:
To be precise, if you used defaults, you can't delete any row in table A
that has rows referencing to it in tables B and C.

On Tue, Nov 30, 2010 at 6:59 AM, manidegr8 <engineer.usman (AT) ymail (DOT) com> wrote:


i am trying to run a query but its not working may be due to constraint
conflicts

i have table A, B and C
B and C have a foreign key linked with A's primary key

so i want to delete an entry from A
for that i hav to delete child records first

can u design a query which wont conflict the constraints..?

Thanks...
Regards.
--
View this message in context:
http://postgresql.1045698.n5.nabble....4p3285654.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql




--
Achilleas Mantzios

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
  #5  
Old   
manidegr8
 
Posts: n/a

Default Re: [SQL] Deleting entries from multiple tables - 11-30-2010 , 04:00 AM



yes i know about on delete cascade
it automatically delete the child when master is deleted

but the database i m working on is not built in this fashion
tables r built on default rules
so constraints r there

i m jus asking to build a procedure or function
that taking from the user and deletes the child and master records against
that input...
i hav tried it on pgadmin 111
but its not working...

please guide me
--
View this message in context: http://postgresql.1045698.n5.nabble....4p3285832.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

--
Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.