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 9164049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:35:17+00:00 2026-06-17T14:35:17+00:00

For example I have 4 tables, all with say userID (which can be referenced

  • 0

For example I have 4 tables, all with say userID (which can be referenced more than once. So for example I can have:

Cars
id, ..., userID  // userID = owner of the car

Garages
id, ..., userID // userID = owner of the garage

Tools
id, ..., userID // userID = owner of the tool

Now I want to do a query to delete the user, but I only want to delete the user if all their related data is gone. In other words, I want to make sure there is no referenced data (let’s say I’m not allowed to have userID = -1 or null. It has to be assigned to a user

The only way I can see to do the checks is:

SELECT count(*) FROM Cars WHERE USERID = userID
SELECT count(*) FROM Garages WHERE USERID = userID
SELECT count(*) FROM Tools WHERE USERID = userID

Where I have to check if any of the results is greater than 0. Is there a way to do this check across N tables in one SQL SELECT query?

  • 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-06-17T14:35:18+00:00Added an answer on June 17, 2026 at 2:35 pm

    If you want to prevent deleting of users that still have cars, garages or tools you should add a foreign key from those tables to the user table.

    If you then try to delete a user which is still referenced you’ll get an error which you can catch and deal with, e.g. by displaying an approriate error message to the user.

    This has the added benefit that the deletion will always be prevented, regardless on how someone tries to delete a user (SQL commandline, a buggy piece of code in your application, …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several tables, let's say for example: articles, thoughts and pages and I
Updated Hello all, MySQL here. Let's say we have 3 tables. TableA , TableB
For example, say I have two tables, Product and Order . Product has ID
say for example i have a tables called table1 and table2; table1{q_id,q_text,date}, table2{a_id,q_id,a_text} I
I have a table, has many rows. for example one of from rows(all rows
I have a table, has many rows. for example one of from rows(all rows
In this example we have 3 related tables on a SQLite database: CREATE TABLE
I have 2 tables in my database, for example: Table1: id (PK), data1 and
I have two tables, for example: Table A Table B ======= ======= Name |
I have 3 tables in my database, for example with such names: TableA, TableB,

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.