From: sebastico on
Hello
I have two Tables.
tblMain(MainID)
tblSec(FamID, Group, MainID)
MainID has same attribute in both tables
tblMain has the correct records.

How can I delete all records from tblSec not present in tblMain? Could you
tell me the kind of query. I'm studying Relational Algebra by myself and this
example would help me to learn
Thanks in advance
From: John Spencer on
Do you want to delete the records from the table or just SHOW the records?

SELECT tblSec.*
FROM tblSec INNER JOIN tblMain
ON tblSec.MainID = tblMain.MainID


DELETE
FROM TblSec
WHERE MainID NOT IN (SELECT MainID FROM tblMain)

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

sebastico wrote:
> Hello
> I have two Tables.
> tblMain(MainID)
> tblSec(FamID, Group, MainID)
> MainID has same attribute in both tables
> tblMain has the correct records.
>
> How can I delete all records from tblSec not present in tblMain? Could you
> tell me the kind of query. I'm studying Relational Algebra by myself and this
> example would help me to learn
> Thanks in advance
From: KenSheridan via AccessMonster.com on
Just to add one thing to John's reply; a NOT IN operation will not work if
the subquery returns at least one NULL. This is because:

x NOT IN(a,b,c)

is the equivalent of:

x<>a AND x <> b AND x <> c

so all three non-equality operations have to evaluate to TRUE. If we
substitute real vales:

42 NOT IN (50, 100, 150)

which is the equivalent of:

42 <> 40 AND 42 <> 100 AND 42 <> 150

then this is fine as it evaluates as:

TRUE AND TRUE AND TRUE

so the whole Boolean expression evaluates to TRUE. But if we substitute:

42 NOT IN (50, NULL, 150)

which is the equivalent of:

42 <> 40 AND 42 <> NULL AND 42 <> 150

then this evaluates as:

TRUE AND NULL AND TRUE

so the expression as a whole evaluates to NULL, neither TRUE nor FALSE.

The way to avoid this is to use the NOT EXISTS predicate:

DELETE *
FROM tblSec
WHERE NOT EXISTS
(SELECT * FROM tblMain
WHERE tblMain.MainID = tblSec.MainID);

The problem with Nulls does not arise with an IN operation of course as
that's the equivalent of:

x = a OR x = b OR x = c

so the expression evaluates to TRUE if any one of the equality operations is
TRUE. Nevertheless the EXISTS predicate will normally perform faster than an
IN operation.

Ken Sheridan
Stafford, England

sebastico wrote:
>Hello
>I have two Tables.
>tblMain(MainID)
>tblSec(FamID, Group, MainID)
>MainID has same attribute in both tables
>tblMain has the correct records.
>
>How can I delete all records from tblSec not present in tblMain? Could you
>tell me the kind of query. I'm studying Relational Algebra by myself and this
>example would help me to learn
>Thanks in advance

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/201005/1

From: sebastico on

John And Ken

Many thanks for you help.
John your sql code works and the information by Ken is very useful as well
"John Spencer" wrote:

> Do you want to delete the records from the table or just SHOW the records?
>
> SELECT tblSec.*
> FROM tblSec INNER JOIN tblMain
> ON tblSec.MainID = tblMain.MainID
>
>
> DELETE
> FROM TblSec
> WHERE MainID NOT IN (SELECT MainID FROM tblMain)
>
> John Spencer
> Access MVP 2002-2005, 2007-2010
> The Hilltop Institute
> University of Maryland Baltimore County
>
> sebastico wrote:
> > Hello
> > I have two Tables.
> > tblMain(MainID)
> > tblSec(FamID, Group, MainID)
> > MainID has same attribute in both tables
> > tblMain has the correct records.
> >
> > How can I delete all records from tblSec not present in tblMain? Could you
> > tell me the kind of query. I'm studying Relational Algebra by myself and this
> > example would help me to learn
> > Thanks in advance
> .
>