473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2523
On Mon, Nov 10, 2003 at 14:31:00 +0900,
Alex <al**@meerkatso ft.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**@meerkatso ft.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**@meerkatso ft.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*******@postg resql.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**@meerkatso ft.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*******@postg resql.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**@meerkatso ft.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
5769
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 use of checkpoints (for simple recovery and Backup Log for full recovery). On several website people speak about full transaction log and the pace of growing can't keep up with the update. Therefore we want to create a script which flushes the...
1
6116
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 records in the subform? I have tried undoing both the main form and the subform and I have tried deleting the record in the main form. Thanks! Mark
3
2001
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 records the database is _huge_. Like 500 meg, although the csv files are only 25 meg in total. When I compact the database its still 100meg. How do I keep the size down? Second, I did a manual delete of 300,000 records yesterday by doing a...
1
2397
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 about 80 records from the Orders table. 80 out of a 1000 records. Now our data entry form shows our customer addresses, but not customer order history. When looking at all of the tables, customer, payments, orders, they still have all of the...
13
9538
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 Dir(strLDB) <> "" Then Kill (strLDB) where strLDB is the path to the ldb file. The client advises that the ldb doesn't lurk after the program closes. Any ideas?
1
8121
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 My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net. Does anyone has an idea on how to make this code compile in .Net ? Thanks, Joannes Vermorel
4
1227
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 action, which is deleting/updating the records that have been already deleted/updated. Is there any way to get around this beside redirecting to another page? Thanks.
11
3681
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) and the 'employee name'. There is another table which assigns an ID to the Shifts, i.e. 1,2 and 3 for morn, eve & night shifts respectively. From the mother table, the incentive is calculated datewise for each employee as per his shift duty. In...
4
3988
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 example, if i delete/update a record from the employee table, the other tables that are referenced to it will be updated/deleted Here is the database create SQL i have CREATE TABLE Employee ( ssn NUMBER (4) PRIMARY KEY, salary ...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.