473,408 Members | 1,866 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,408 software developers and data experts.

Delet query with joined tables

Hi,

I've run into this problem many times and have not found a good
solution yet.

Here's what I have:

table ParentTable - the "1" table
table ChildTable- the "many" table
ParentTable inner joined to ChildTable on ParentTable.ParentID =
ChildTable.ParentID_f

Here's what I would like to do but can't:

delete ChildTable.* from ChildTable inner join ParentTable on
ChildTable.ParentID_f = ParentTable.ParentID where
ParentTable.SomeField = 'something';

There are too many records in the ParentTable to make the following
practical, i.e. its VERY slow, so I'd like to avoid it (or is this
actually the best way?):

delete ChildTable.* from ChildTable where ChildTable.ParentID_f in
(Select ParentID from ParentTable where SomeField = 'something');

I'm interested in hearing how others handle this issue.

Aug 21 '06 #1
4 2007
If you have a one to many relationship between the Parent and child
table then you can create a relationship between the 2 tables in the
relationship window (which is on the menu bar when you are in the Tables
tab). In the relationship you specify Cascade Delete. When you delete
a row in the parent table all of the corresponding children rows will
also get deleted. Make sure you have a backup of your db first before
you delete the rows incase you delete the wrong rows.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 21 '06 #2

Rich P wrote:
If you have a one to many relationship between the Parent and child
table then you can create a relationship between the 2 tables in the
relationship window (which is on the menu bar when you are in the Tables
tab). In the relationship you specify Cascade Delete. When you delete
a row in the parent table all of the corresponding children rows will
also get deleted. Make sure you have a backup of your db first before
you delete the rows incase you delete the wrong rows.
Thats true, but in the case I described above, I only want to delete
from the child table. I generally need to do this only in design and
development testing, but I can think of a case where - in production -
you may want to delete from the child only: if you have a large volume
of transaction or event-logging type data that looses its functional
utility over time for whatever reason, it would be advantageous to
archive it periodically based on criteria not contained entirely within
the child table. This is what I'm trying to figure out how to do. In
the past I've used code, but I'm wondering if there's some way to do
this using only SQL without using "in (...)".

Aug 21 '06 #3
Frank,

I tested this syntax on a case where some 400 rows are deleted out of a
joined table with about 10.000 rows and Access deleted the rows within
a second on my laptop. It is possible the operation could be slow
because of a failing index on at least Childtable.ParentID_f. A key on
ParentTable.Somefield also could help. Do you make use of referential
integrity ? I suppose not because it automatically will create useful
indexes. Performance also can suffer by a lot of reasons (heterogenous
join with another database-brand, Network-performance, locking by
multiple users or many thousands of records in a huge access-database).
In my experience almost always lack of indexes is the main
performance-killer. In my experience modifying thousand of records in
an table with more than a million records can be done within ten
minutes using proper SQL by a selfmade tool that analyses all records
and fields through VBA comparing with another table of a more than a
million records. Both your SQL-solutions will work, but I think the
first is in most cases probably the most efficient. When created with
the Query-editor using QBE and specifying a delete-query the
somefield-field is also listed but actually not deleted because only
all fields of a table (one or more rows) can be deleted and not just
one field. Access just doesn't show the exclusion-checkbox because the
delete-statement will only delete table-rows indicated with the
"table.*" identifier.

Marc

Frank List schreef:
Hi,

I've run into this problem many times and have not found a good
solution yet.

Here's what I have:

table ParentTable - the "1" table
table ChildTable- the "many" table
ParentTable inner joined to ChildTable on ParentTable.ParentID =
ChildTable.ParentID_f

Here's what I would like to do but can't:

delete ChildTable.* from ChildTable inner join ParentTable on
ChildTable.ParentID_f = ParentTable.ParentID where
ParentTable.SomeField = 'something';

There are too many records in the ParentTable to make the following
practical, i.e. its VERY slow, so I'd like to avoid it (or is this
actually the best way?):

delete ChildTable.* from ChildTable where ChildTable.ParentID_f in
(Select ParentID from ParentTable where SomeField = 'something');

I'm interested in hearing how others handle this issue.
Aug 21 '06 #4

MarcHG wrote:
Frank,

I tested this syntax on a case where some 400 rows are deleted out of a
joined table with about 10.000 rows and Access deleted the rows within
a second on my laptop. It is possible the operation could be slow
because of a failing index on at least Childtable.ParentID_f. A key on
ParentTable.Somefield also could help. Do you make use of referential
integrity ? I suppose not because it automatically will create useful
indexes. Performance also can suffer by a lot of reasons (heterogenous
join with another database-brand, Network-performance, locking by
multiple users or many thousands of records in a huge access-database).
In my experience almost always lack of indexes is the main
performance-killer. In my experience modifying thousand of records in
an table with more than a million records can be done within ten
minutes using proper SQL by a selfmade tool that analyses all records
and fields through VBA comparing with another table of a more than a
million records. Both your SQL-solutions will work, but I think the
first is in most cases probably the most efficient. When created with
the Query-editor using QBE and specifying a delete-query the
somefield-field is also listed but actually not deleted because only
all fields of a table (one or more rows) can be deleted and not just
one field. Access just doesn't show the exclusion-checkbox because the
delete-statement will only delete table-rows indicated with the
"table.*" identifier.

Marc
Well here's what lead to my post in the first place, since you
mentioned the case of deleting from tables with millions of records-
thats exactly what I'm doing. I'm stress testing a new SQL Server
database schema where the "child" table could eventually have 10
million+ records and the "parent" table may reach into the upper 6
figures. Eventually I'll want to archive the "child" table data, but
not the "parent" table data, so I will probably be doing occasional
massive deletes as SOP. I might try your approach using VBA. Or I
might do a strategic denormalization and store all of the deletion
criteria data within the "child" table itself- I'll have to think about
that one.

Aug 21 '06 #5

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
2
by: sqlgoogle | last post by:
Hi I'm having update problem. Here is the senario I have to different db server (SQL Server) linked with each other In DB Server 1 I have 2 tables & In DB Server 2 I have 3 tables. I have...
2
by: allyn44 | last post by:
Hello--I have 2 tables (illness,event) that a need to query and create a recordset The key fields are personId and description (text field) in each table. I also have other needed fields in the...
1
by: Michael | last post by:
I have a query that uses two joined tables. The query contains data. I would like to pull in a data drom another table but when I add that table to the query design and make the join,the existing...
1
by: Carl B Davis | last post by:
Help please!!! I am an intermediate access user that is getting my bottom kicked by what seems an easy problem to fix. I maintain an employee database at work. I have set up a query from two tables...
1
by: Ike | last post by:
I have a simple query of joined tables that is failing to give me any rows of data (though, in checking by hand, it certainly should). Essentially, I am trying to return all rows from `ups` that...
2
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for...
1
by: racquetballer | last post by:
I need to to an update query that involves three tables: table Dealer needs to be updated with data from table Personnel and table Title. Dealer is joined to Personnel where Dealer.Dealer_Code =...
0
bilibytes
by: bilibytes | last post by:
Hi there, I am wondering if there is a possibility to declare a Model_DbTable_Table1JoinedToTable2ToTable3... class It would be convenient to create compound table from several tables joined...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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,...
0
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...
0
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...

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.