473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem Deleting Referenced records

Hi,
I have two tables , A and B where table B has a foreign key constraint
to table A.

I want to delete all records in table A that are older than a certain
date that are not referenced by table B.

When I use a DELETE FROM the entire transaction fails as soon as a
referential integrity violation is detected.

Is that the normal behavior? Is there a way to allow the deletion to
complete but skipping all those records that are referenced ?

Thanks in advance for any help.

Alex

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
5 2497
On Mon, Nov 10, 2003 at 14:31:00 +0900,
Alex <al**@meerkatsoft.com> wrote:
Hi,
I have two tables , A and B where table B has a foreign key constraint
to table A.

I want to delete all records in table A that are older than a certain
date that are not referenced by table B.

When I use a DELETE FROM the entire transaction fails as soon as a
referential integrity violation is detected.

Is that the normal behavior? Is there a way to allow the deletion to
complete but skipping all those records that are referenced ?


I think you want to do something like:

delete from A where A.stamp < current_date - '1 month' and
not exists (select 1 from B where B.Aid = A.id);

It is also possible to join A and B on the delete command using a nonstandard
syntax, but not exists should be about as fast.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #2
Bruno,
thanks. I actually did it that way but having to join two tables each
1-2 million records makes this process rather time consuming.
I was hoping that the ON DELETE options in the constraint could handle
that.

It seems to be a bit odd that if I want to delete 100 records that are
not related to each other, and one record deletion fails that then the
entire delete process fails.

.... but I guess there is nothing I can do about.

Alex

Bruno Wolff III wrote:
On Mon, Nov 10, 2003 at 14:31:00 +0900,
Alex <al**@meerkatsoft.com> wrote:

Hi,
I have two tables , A and B where table B has a foreign key constraint
to table A.

I want to delete all records in table A that are older than a certain
date that are not referenced by table B.

When I use a DELETE FROM the entire transaction fails as soon as a
referential integrity violation is detected.

Is that the normal behavior? Is there a way to allow the deletion to
complete but skipping all those records that are referenced ?


I think you want to do something like:

delete from A where A.stamp < current_date - '1 month' and
not exists (select 1 from B where B.Aid = A.id);

It is also possible to join A and B on the delete command using a nonstandard
syntax, but not exists should be about as fast.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #3
On Mon, Nov 10, 2003 at 16:20:21 +0900,
Alex <al**@meerkatsoft.com> wrote:
Bruno,
thanks. I actually did it that way but having to join two tables each
1-2 million records makes this process rather time consuming.
I was hoping that the ON DELETE options in the constraint could handle
that.
If only a small number of the 1-2 million records have old dates, than the
where not exists method might be faster. An index scan could be used
to find the records with old dates and then for each record an index
lookup could be done in table B to see if it should really be deleted.

It seems to be a bit odd that if I want to delete 100 records that are
not related to each other, and one record deletion fails that then the
entire delete process fails.


You can delete each record in its own transaction if you want that
behavior.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #4
Bruno,
I am not sure why but the whole delete proces with the where not exists
method took 3hrs, rather long I would say.

Table A had 2.5mil records
Table B had about 30k records

In addition Table C with about 2 mil records referenced a referenced A
(same key as B)
In both A and B about 150k records where deleted... but the process took
more than 3 hrs.

Its pretty long I would say. I noticed in the past that if I had
multiple foreign keys, referencing different tables like TableA <--
TableB <-- TableC then deletes are really slow... sometimes in the area
of one delete per second. Never really figured out why. (And yes I did
run a Vacuum or Vacuum analyze on the DB
or Tables).

Alex

Bruno Wolff III wrote:
On Mon, Nov 10, 2003 at 16:20:21 +0900,
Alex <al**@meerkatsoft.com> wrote:

Bruno,
thanks. I actually did it that way but having to join two tables each
1-2 million records makes this process rather time consuming.
I was hoping that the ON DELETE options in the constraint could handle
that.


If only a small number of the 1-2 million records have old dates, than the
where not exists method might be faster. An index scan could be used
to find the records with old dates and then for each record an index
lookup could be done in table B to see if it should really be deleted.
It seems to be a bit odd that if I want to delete 100 records that are
not related to each other, and one record deletion fails that then the
entire delete process fails.


You can delete each record in its own transaction if you want that
behavior.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #5
On Wed, Nov 12, 2003 at 00:08:02 +0900,
Alex <al**@meerkatsoft.com> wrote:
Bruno,
I am not sure why but the whole delete proces with the where not exists
method took 3hrs, rather long I would say.

Table A had 2.5mil records
Table B had about 30k records

In addition Table C with about 2 mil records referenced a referenced A
(same key as B)
In both A and B about 150k records where deleted... but the process took
more than 3 hrs.

Its pretty long I would say. I noticed in the past that if I had
multiple foreign keys, referencing different tables like TableA <--
TableB <-- TableC then deletes are really slow... sometimes in the area
of one delete per second. Never really figured out why. (And yes I did
run a Vacuum or Vacuum analyze on the DB
or Tables).


If you do a lot of deleting of rows in referenced tables, then you probably
want to create indexes in the referencing tables for the key. This will
allow for quicker checks by the referential integrity checking triggers.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the...
1
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the...
3
by: Mike Turco | last post by:
I'm working on an application that imports and exports tons of CSV data, like 64,000 records per file, and six or seven files in a set. First off, by the tine I import a few hundred thousand...
1
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked pretty well up until I made the mistake of deleting...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
4
by: JN | last post by:
Hi all, I have some pages where I need to delete/update database records. Everything is fine until I hit the refresh button on the browser (IE). This will cause the pages to repeat the last...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
4
by: Adrock952 | last post by:
I am trying to create my tables where if i delete/update a record from one table, all the other tables are affected by deleting/updating any records that reference the original record. For...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.