Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

sql - How to Delete Records NOT IN

Hi I have the following SQL Query which gives me Scheme_Id which exist both in ProjectSchemes and Schemes table. I want to delete all records from Schemes table which have no record to ProjectSchemes table. How can I do so? Please help. I'm using MSSQL

select scheme_id from Schemes where Scheme_Id
in(select s.Scheme_Id from Projects p 
inner join ProjectSchemes ps on ps.Project_Id=p.Project_Id
inner join Schemes s on s.Scheme_Id=ps.Scheme_Id)

I'm trying to do the following but it's not working. Not working means no records affected but as I checked my Schemes table there are so many records that their scheme_id cannot be found on the ProjectSchemes table

delete from Schemes where Scheme_Id
not in(select s.Scheme_Id from Projects p 
inner join ProjectSchemes ps on ps.Project_Id=p.Project_Id 
inner join Schemes s on s.Scheme_Id=ps.Scheme_Id)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would like to start with assumptions.

  1. You have a chainlike data model: Projects --* ProjectSchemes --* Schemes
  2. Your target is to have only valid chains, so no ProjectSchemes without Project, no Schemes without ProjectSchemes.
  3. NULL is not a valid value for one of your ids.
  4. All ids are unique in their table
  5. You don't use referential integrity mechanisms of your database

As a result your SELECT would list the scheme_id for all Schemes in the Schemes table.

Said that, you should start to delete all ProjectSchemes without a corresponding Project. These are ProjectSchemes with an id of NULL or an id which does not exists in the Projects Table:

DELETE ProjectSchemes WHERE (Project_Id is NULL) OR 
(NOT EXISTS (SELECT * FROM Projects WHERE
             Projects.Project_Id = ProjectSchemes.Project_Id))

After deleting the ProjectsSchemes without a Project we now may have some new orphans in the Schemes Table. The next thing is now to delete all Schemes which have an id of NULL or an id which does not exists in the ProjectsSchemes Table:

DELETE Schemes WHERE (Scheme_Id is NULL) OR 
(NOT EXISTS (SELECT * FROM ProjectSchemes WHERE
             ProjectSchemes.Scheme_Id = Schemes.Scheme_Id))

There is still a chance to have schemes which are not connected to a project without deleting the ProjectSchemes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...