Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:27:02+00:00 2026-05-19T10:27:02+00:00

I have 3 tables: Files (primary key = FileID) Accounts (primary key = AccountID)

  • 0

I have 3 tables:

Files (primary key = FileID)
Accounts (primary key = AccountID)
AccFiles (2 fields: FileID and AccountID)

The AccFiles table links the primary keys of the Files table to the Accounts table.

In my scenario, if there is an Account record there will also be 1 corresponding File in the Files table and a reference to both in the AccFiles table. I guess this is a 1 to 1 relationship. Unfortunately due to a bug some files got deleted but their Account and the record in the AccFiles table didn’t. I need to write a SQL script that will delete records from the Accounts table and AccFiles table where there is no longer a corresponding File (as referenced by the Accfiles FileID).

I’m using SQL Server and have no idea how to go about doing this. Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:27:02+00:00Added an answer on May 19, 2026 at 10:27 am

    This will delete all records from AccFiles whose FileId field does not exist in the File table then delete all records from Accounts whose AccountId field does not exist in the AccFiles table:

    DELETE AccFiles
    WHERE FileID
    NOT IN
    (
      SELECT FileID
      FROM Files
    );
    
    DELETE Accounts
    WHERE AccountId
    NOT IN
    (
      SELECT AccountId
      FROM AccFiles
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables Table1: Plant Table PlantId(Primary key) Plantname and some other fields
I have two tables: Folder, Files. Each table has 4 fields: ParentID, ID, Name,
In my application I have small tables(files) with unique keys (most of those have
I have the following 3 tables: (PK = Primary Key, FK = Foreign Key)
I have an table which use an auto-increment field (ID) as primary key. The
I have two tables in Sql Server, one containing IDs for files and the
I have all my tables, views, triggers in different files (Table1.sql, Table2.sql, View1.sql,...), and
I have a table in SQL Server 2005. alt text http://www.techpint.com/sites/default/files/images/table.JPG I want to
I have a table that acts as a dropbox for files that get automatically
I have a table, called 'files', where I store file_id's. Basically, users on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.