473,750 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Par entID =
ChildTable.Pare ntID_f

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

delete ChildTable.* from ChildTable inner join ParentTable on
ChildTable.Pare ntID_f = ParentTable.Par entID where
ParentTable.Som eField = '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.Pare ntID_f in
(Select ParentID from ParentTable where SomeField = 'something');

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

Aug 21 '06 #1
4 2037
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.Pare ntID_f. A key on
ParentTable.Som efield 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.Par entID =
ChildTable.Pare ntID_f

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

delete ChildTable.* from ChildTable inner join ParentTable on
ChildTable.Pare ntID_f = ParentTable.Par entID where
ParentTable.Som eField = '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.Pare ntID_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.Pare ntID_f. A key on
ParentTable.Som efield 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
3070
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 solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche Nachricht-----
2
595
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 joined tables with each other first & then between the servers When I run select on DB Server1 with both tables joined I'm getting more values then when I run the select between the DB Servers (about 20
2
3456
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 query but these are the 2 that join the 2 tables. I also have unique id's for each table (autoID, SeqNum). It runs ok except one issue--I have some unwanted duplicates in the result-- For each ID in either table--the description may not be unique ...
1
2095
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 data is not shown in the query in datasheet view. I have realised that this is because the joined fields in the existing data were not populated to link to the newly added table. I can understand why data was not pulled in from the new table...
1
1703
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 to generate a query displaying useful fields, but only for those whom appear on both tables. The names are formatted different so I chose to join them using the SSN. I set up the query and joined the SSN property but when I run the query, it...
1
1947
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 have `floattime` not equal to it's default value of '0' (please not, in this db dates and times are saved as varchars - so essentially I am comparing strings) SELECT ups.date,associates.branch ,associates.username ,ups.time ,ups.floattime...
2
2396
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 suffix ID where 1=Phd, 2= MD. To display all of these to the user, I created a form with an underlying query. The problem I am encountering is this, when we have an empty field, for example where ID="", the query returns nothing. How do i work around...
1
15762
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 = Personnel.so_cd Personnel is linked to Title by Personnel.name_id = Title.name_id I need to update fields Principal_First_Name, Principal_Last_Name, and Company_Email in Dealer from FirstName, LastName, and InternetEmailAddr in Personnel where...
0
1714
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 from a foreign key. Lets say we have: tables: -------------------------------------------
0
9397
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...
0
9257
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
8264
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
6810
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
4716
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...
0
4893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3327
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
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.