Обсуждение: Finding missing records...

Поиск
Список
Период
Сортировка

Finding missing records...

От
Matthew Hagerty
Дата:
Greetings,

I have two tables that maintain a 1:1 relationship via an int based primary 
key.  A record in the primary table should never exist without an 
associated record in the secondary table, however somehow this has 
happened.  Can someone demonstrate a query that would show all records in 
the primary table that do not have associated records in the secondary table?

Thanks,
Matthew



Re: [SQL] Finding missing records...

От
wieck@debis.com (Jan Wieck)
Дата:
> Greetings,
>
> I have two tables that maintain a 1:1 relationship via an int based primary
> key.  A record in the primary table should never exist without an
> associated record in the secondary table, however somehow this has
> happened.  Can someone demonstrate a query that would show all records in
> the primary table that do not have associated records in the secondary table?
   SELECT att_pk FROM tab_pk WHERE NOT EXISTS       (SELECT att_fk FROM tab_fk WHERE att_fk = att_pk);

   This is the SQL3 referential definition of PENDANT. It'll not   be in the FOREIGN KEY support of 7.0, but we'll
discussif we   implement  it  for 7.1 (even if it will be damned slooooow if   done on every delete/update to an FK
table).


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#========================================= wieck@debis.com (Jan Wieck) #




Re: [SQL] Finding missing records...

От
Sevo Stille
Дата:
Matthew Hagerty wrote:
> happened.  Can someone demonstrate a query that would show all records in
> the primary table that do not have associated records in the secondary table?

EXCEPT. As in "select key from table1 except select key_ref from
table2;". 

Sevo


-- 
sevo@ip23.net