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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:14:39+00:00 2026-05-13T12:14:39+00:00

We have a user table which contains all the users from the company. As

  • 0

We have a user table which contains all the users from the company. As an administrator you have the permission to delete user entries. The UI shows all the users with a checkbox per row to indicate if it needs to be deleted. It’s quite possible that other tables have foreign key relationship(s) with the user table and hence would prevent deletion, by throwing a foreign key constraint violation.

What would be the best practice to handle deletion in such cases, should the UI not show the checkbox if there could be other records which are dependent on this user which actually means that while rendering this user list page you need to do additional checks per row to find out if the checkbox needs to be enabled or not

What would be the best practice in this case.

  • 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-13T12:14:39+00:00Added an answer on May 13, 2026 at 12:14 pm

    First, I’d be surprised if you are allowed to DELETE the user records. It’s more likely that you mark them locked or no-login-allowed or something. Sometimes it’s important to preserve the data for historical purposes.

    Second, if you really do need to delete the rows (in this scenario or some other scenario) you should read about foreign keys with the ON DELETE CASCADE or other options.

    See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

    When you delete the parent row (users), you can declare your desired behavior in foreign key constraints in dependent tables:

    • RESTRICT, meaning don’t delete the parent if there are dependent rows
    • CASCADE, meaning delete any dependent rows automatically and atomically
    • SET NULL, meaning change the value in the foreign key column in the child table to NULL
    • SET DEFAULT, meaning change the value in the foreign key column to the default defined for that column

    Of course these constraints work only in InnoDB. Foreign keys are not supported in MyISAM.


    Re your comment: No, there’s no way to quickly check for the presence of dependent rows referencing a given user row. These sort of stats would have to be managed on a user-by-user basis. So it would contain data on the order of replicating the dependent rows themselves. At that point, you might as well query the data directly, instead of the hypothetical statistics. Here’s how I’d do it:

    SELECT u.*
    FROM Users u
    LEFT OUTER JOIN SomeChildTable1 c1 ON (u.user_id = c1.user_id)
    LEFT OUTER JOIN SomeChildTable2 c2 ON (u.user_id = c2.user_id)
    ...other tables
    WHERE c1.user_id IS NULL AND c2.user_id IS NULL AND ...other terms
    

    By using outer join, the columns in c1, c2, etc. are NULL when there’s no match, and the columns in Users are returned whether there’s a match or not. So the query returns only those Users that have no dependent rows.

    Some people shy away from joins because they were told in some magazine article that joins are costly, but compared to other solutions to accomplish what you want, the joins don’t look so bad. In this case, it can take advantage of an index on the user_id column in those foreign keys and not have to read the row data at all.

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

Sidebar

Related Questions

I have a table of data sorted by date, from which a user can
I have a user table in my database which contains two columns FirstName and
I have table Users which contains columns: id, name, points, extra_points. How can I
I have a table User which has an identity column UserID , now what
When designing user table what would be the must have fields from the security/user
I have a field in a table which contains bitwise flags. Let's say for
I have a table called order which contains columns id , user_id , price
I have a couple of tables which are used to log user activity for
I have a user table in my mysql database that has a password column.
We have a user table, every user has an unique email and username. We

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.