Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete from one table where key field does not exist in another

Status
Not open for further replies.

Rick7

IS-IT--Management
Apr 15, 2003
16
US
Hey all, I have two tables. Each has the same primary key. There is a one-to-one relationship with these two tables. However somehow, they got out of sync and I have more records in one table than the other...

I would like to scan (select) records from table1, and delete records from table 2 if a corresponding record is not found. I know that there must be a way to do this without needing to use a scripting language.

I am a seasoned 4GL database programmer, but kind of a newbie at SQL. Any help would be appreciated.
 
with sql
Code:
delete from table2 where table2key not in (select table1key from table1)

I think something like this but I have to admit I'm a bit drunk right now so be cautioes ;)



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top