View Single Post
  #3  
Old May 28th, 2010, 10:36 PM posted to microsoft.public.access.queries
KenSheridan via AccessMonster.com
external usenet poster
 
Posts: 1,610
Default Delete records from second table

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:

xa 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/For...eries/201005/1