473,729 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_numb er = c.customer_numb er
where c.customer_numb er 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 1875
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_NUMB ER = P.CUSTOMER_NUMB ER
)
)

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_numb er = c.customer_numb er
where c.customer_numb er 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_numb er = c.customer_numb er
where c.customer_numb er 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
1738
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 self-extracting executable update programs in a famous installer style with adjustable user-friendly interface and multilingual support. Enhanced with features like easy-to-use interface including a Wizard mode, powerful patch engine, integrated...
10
2052
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 these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
2
1643
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 smallest_node); const Quadtree_Node *& GetTopLeft(); Bounding_Box m_bounds; // Bounding Coordinates of this node
6
1516
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 http://www.cs.kuleuven.ac.be/~ares/tmp/array_2_hpp.html It is a 2d array that dynamically allocates its storage. It cannot be resized. It offers element access methods for both sequential access and 2d element access.
27
3847
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 failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
4
3591
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 to modify existing data with in my DB/MSDE. In general, I have textbox's filled with existing data from my "Employee" table. I have amongst other buttons, an "Update" button. Where, when viewing, if I want to change ie the persons name, anyother...
2
2318
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 *int_obj; public: explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
12
2850
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 so often. But we would like to put something in place that will do this automatically. The reason for doing this is to ultimately improve the application's performance that writes the transactions to this file. When the file gets to be over about...
1
1462
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 with the aim of specifying one for eventual deletion. It returns the "row deleted" echo fine, but doesn't actually delete anything. <?
0
8913
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
8761
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
9426
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...
1
9200
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
9142
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
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
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();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.