473,406 Members | 2,451 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,406 software developers and data experts.

Easy & safe deletion of inconsistent rows

Hi all

Lets say we have two tables:

Customer:
Customer_number : Decimal(15)
Name : Char(30)

Purchase:
Purchase_number : Decimal(15)
Customer_number : Decimal(15)

There is a relationship between Purchase and Customer, so that each
purchase must belong to a customer. That is, Customer_number in the
Purchase table must match a row in the Customer table (with the same
customer_number) - a foreign key.

This relationship is not enforced by the database.

One day you discover, that not all purchases has a relationship to an
existing customer. That is, the database is inconsistent. First you make a
select to find the inconsistent rows:

select *
from Purchase p
left outer join Customer c
on p.customer_number = c.customer_number
where c.customer_number is null

Lets say, you find 20 similar unenforced relationships (between other
tables). All containing inconsistent rows. You make 20 select statements
to discover all the inconsistencies.

The next step is to delete the inconsistencies. And this is my question.
How do one safely (be sure to only delete inconsistent rows) and easily
delete the inconsistencies? It is a requirement, that one would be able to
see which rows has been deleted. You have a typical IBM MVS/TSO
environment at your disposal. That is DB2, COBOL, JCL, Spufi, QMF, ...
Greetings,

Mads
Sep 26 '06 #1
3 1856
Hi.

I'm in an LUW environment. Assuming you have modifying table functions
at your disposal, I'd try something like the following (you might want
to turn off autocommit first, if that applies):

SELECT
*
FROM
OLD TABLE
(
DELETE FROM
PURCHASES P
WHERE
NOT EXISTS
(
SELECT
1
FROM
CUSTOMERS C
WHERE
C.CUSTOMER_NUMBER = P.CUSTOMER_NUMBER
)
)

If modifying table functions aren't available in IBM MVS/TSO, then
you'd have to execute a separate query first (just as you already did).
The DELETE should translate as-is, though.

--Jeff

codeman wrote:
Hi all

Lets say we have two tables:

Customer:
Customer_number : Decimal(15)
Name : Char(30)

Purchase:
Purchase_number : Decimal(15)
Customer_number : Decimal(15)

There is a relationship between Purchase and Customer, so that each
purchase must belong to a customer. That is, Customer_number in the
Purchase table must match a row in the Customer table (with the same
customer_number) - a foreign key.

This relationship is not enforced by the database.

One day you discover, that not all purchases has a relationship to an
existing customer. That is, the database is inconsistent. First you make a
select to find the inconsistent rows:

select *
from Purchase p
left outer join Customer c
on p.customer_number = c.customer_number
where c.customer_number is null

Lets say, you find 20 similar unenforced relationships (between other
tables). All containing inconsistent rows. You make 20 select statements
to discover all the inconsistencies.

The next step is to delete the inconsistencies. And this is my question.
How do one safely (be sure to only delete inconsistent rows) and easily
delete the inconsistencies? It is a requirement, that one would be able to
see which rows has been deleted. You have a typical IBM MVS/TSO
environment at your disposal. That is DB2, COBOL, JCL, Spufi, QMF, ...
Greetings,

Mads
Sep 26 '06 #2
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it. Here are my guessses at what you meant:

CREATE TABLE Customers
(cust_nbr DECIMAL (15) NOT NULL PRIMARY KEY,
cust_name CHAR (30) NOT NULL)

CREATE TABLE Purchases
(purchase_nbr DECIMAL (15) NOT NULL PRIMARY KEY,
cust_nbr DECIMAL (15) NOT NULL
REFERENCES Customers (cust_nbr)
ON UPDATE CASCADE);

or did you mean:

CREATE TABLE Purchases
(purchase_nbr DECIMAL (15) NOT NULL,
cust_nbr DECIMAL (15) NOT NULL
REFERENCES Customers (cust_nbr)
ON UPDATE CASCADE,
PRIMARY KEY (purchase_nbr, cust_nbr));
>This relationship is not enforced by the database. <<
The guy that failed to add the REFERENCES clause and caused this
problem has been fired by now, right? Likewise the use of singular
table names has been corrected.
>The next step is to delete the inconsistencies. <<
DELETE FROM Purchases
WHERE NOT EXISTS
(SELECT *
FROM Customers AS C
Purchases.cust_nbr = P.cust_nbr);
>It is a requirement, that one would be able to see which rows has been deleted. <<
You that before with

SELECT P.*
FROM Purchases AS P
WHERE NOT EXISTS
(SELECT *
FROM Customers AS C
P.cust_nbr = P.cust_nbr);

An EXISTS () is usually faster than the LEFT OUTER JOIN you were using;
there is no need to materialize a result set.

The final is to add the missing constraints -- mop the floor AND fix
the leak!

Sep 27 '06 #3

codeman wrote:
Hi all

Lets say we have two tables:

Customer:
Customer_number : Decimal(15)
Name : Char(30)

Purchase:
Purchase_number : Decimal(15)
Customer_number : Decimal(15)

There is a relationship between Purchase and Customer, so that each
purchase must belong to a customer. That is, Customer_number in the
Purchase table must match a row in the Customer table (with the same
customer_number) - a foreign key.

This relationship is not enforced by the database.

One day you discover, that not all purchases has a relationship to an
existing customer. That is, the database is inconsistent. First you make a
select to find the inconsistent rows:

select *
from Purchase p
left outer join Customer c
on p.customer_number = c.customer_number
where c.customer_number is null

Lets say, you find 20 similar unenforced relationships (between other
tables). All containing inconsistent rows. You make 20 select statements
to discover all the inconsistencies.

The next step is to delete the inconsistencies. And this is my question.
How do one safely (be sure to only delete inconsistent rows) and easily
delete the inconsistencies? It is a requirement, that one would be able to
see which rows has been deleted. You have a typical IBM MVS/TSO
environment at your disposal. That is DB2, COBOL, JCL, Spufi, QMF, ...
Greetings,

Mads
The others have made appropriate suggestions about the technical
aspects, but I question the business logic. Why have you got purchases
orphaned from customers, are the purchases real? If so then you should
be looking to reinstate the associated customer details, if only with
just the name that is available from the purchase details. Even if
this is just a training exercise, the principle still holds, if the
details are real facts, then deleting them because they are orphaned
just creates more trouble down the line.

Robert

Sep 27 '06 #4

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

Similar topics

0
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size...
10
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of...
2
by: Christopher Pisz | last post by:
currently my node is like so with everything public: class Quadtree_Node { public: Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds); ~Quadtree_Node(); void Subdivide(float...
6
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at ...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
4
by: MikeY | last post by:
Hi everyone, I have posted earlier this week, but I'm still scratching my head trying to figure out how to change/modify my data back to my db. Using C# Windows forms. I am trying to learn how...
2
by: Kuba_O | last post by:
Hello, i've got simple question about std::auto_ptr: what makes it is exceptions safe? Lets say i have class "int_smart_ptr" implemented like this: class int_smart_ptr { private: int...
12
by: weeodett | last post by:
Is there a way to automate the deletion of the oldest rows in a transaction log file when the file reaches a certain number of rows? Currently, we are simply deleting the oldest rows manually every...
1
by: Philth | last post by:
Hello folks, Hopefully this will be an easy one for most of you guys. I don't delve into PHP much so I'd appreciate some help. The aim of the script below is to display a collection of rows...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.