Thread: Deleting entries from multiple tables
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.com/Deleting-entries-from-multiple-tables-tp3285654p3285654.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
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@ymail.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.com/Deleting-entries-from-multiple-tables-tp3285654p3285654.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
<font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">Tobe precise, if you used defaults, you can't delete any row in table A that has rows referencing to it in tablesB and C.<br /></span></font><br /><div class="gmail_quote">On Tue, Nov 30, 2010 at 6:59 AM, manidegr8 <span dir="ltr"><<ahref="mailto:engineer.usman@ymail.com">engineer.usman@ymail.com</a>></span> wrote:<br /><blockquote class="gmail_quote"style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br /> i am trying to run a querybut its not working may be due to constraint<br /> conflicts<br /><br /> i have table A, B and C<br /> B and C havea foreign key linked with A's primary key<br /><br /> so i want to delete an entry from A<br /> for that i hav to deletechild records first<br /><br /> can u design a query which wont conflict the constraints..?<br /><br /> Thanks...<br/> Regards.<br /> --<br /> View this message in context: <a href="http://postgresql.1045698.n5.nabble.com/Deleting-entries-from-multiple-tables-tp3285654p3285654.html" target="_blank">http://postgresql.1045698.n5.nabble.com/Deleting-entries-from-multiple-tables-tp3285654p3285654.html</a><br />Sent from the PostgreSQL - sql mailing list archive at Nabble.com.<br /><font color="#888888"><br /> --<br /> Sent viapgsql-sql mailing list (<a href="mailto:pgsql-sql@postgresql.org">pgsql-sql@postgresql.org</a>)<br /> To make changesto your subscription:<br /><a href="http://www.postgresql.org/mailpref/pgsql-sql" target="_blank">http://www.postgresql.org/mailpref/pgsql-sql</a><br/></font></blockquote></div><br />
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 : maker of 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 ON DELETE 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 έγραψε: > 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@ymail.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.com/Deleting-entries-from-multiple-tables-tp3285654p3285654.html > > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > > > -- > > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > > To make changes to your subscription: > > http://www.postgresql.org/mailpref/pgsql-sql > > > -- Achilleas Mantzios
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.com/Deleting-entries-from-multiple-tables-tp3285654p3285832.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com.