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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:07+00:00 2026-06-16T16:47:07+00:00

I want to delete all rows in database, that doen’t have a reference in

  • 0

I want to delete all rows in database, that doen’t have a reference in another table:

this is ok:

mysql> DELETE FROM auth_user WHERE username = 'Sample41';
Query OK, 1 row affected (0.02 sec)

this is not ok:

mysql> DELETE FROM auth_user WHERE username = 'Sample98';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`mydatabase`.`school_subjects`, CONSTRAINT `user_id_refs_id_9e8f6fd7` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))

So my goal is to get a query that will delete all users (they will start with ‘Sample’) that doesn’t have reference in other tables ( actually just one table ‘school_subjects’ )

thank you.,

UPDATE:

mysql> describe school_subjects;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| name             | varchar(500) | NO   |     | NULL    |                |
| user_id          | int(11)      | NO   | MUL | NULL    |                |
| created_by       | varchar(64)  | NO   |     | NULL    |                |
| created_time     | datetime     | NO   |     | NULL    |                |
| num_of_followers | int(11)      | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> describe auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| username     | varchar(30)  | NO   | UNI | NULL    |                |
| first_name   | varchar(30)  | NO   |     | NULL    |                |
| last_name    | varchar(30)  | NO   |     | NULL    |                |
| email        | varchar(75)  | NO   |     | NULL    |                |
| password     | varchar(128) | NO   |     | NULL    |                |
| is_staff     | tinyint(1)   | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | NULL    |                |
| is_superuser | tinyint(1)   | NO   |     | NULL    |                |
| last_login   | datetime     | NO   |     | NULL    |                |
| date_joined  | datetime     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
  • 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-16T16:47:08+00:00Added an answer on June 16, 2026 at 4:47 pm

    So my goal is to get a query that will delete all users (they will
    start with ‘Sample’) that doesn’t have reference in other tables (
    actually just one table ‘school_subjects’ )

    You have a self-referencing table. If you want to delete all records you must include school_subjects and auth_user. Try:

    DELETE FROM auth_user WHERE username not in (SELECT id FROM auth_user) 
    and  username not in (SELECT user_id FROM school_subjects);
    

    UPDATE: If you don’t have a self-referencing constraint and only care about ‘school_subjects’ try the following query, note that it will delete all records that are not in conflict with constraints:

    DELETE FROM auth_user WHERE username not in (SELECT user_id FROM school_subjects);
    

    If you need to delete just a few you will need:

    DELETE FROM auth_user 
    WHERE username in ('SomeRecordToBeDeleted', 'one more') AND
    username not in (SELECT user_id FROM school_subjects);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove all rows that i entered from my SQLite database table.
I have a table that get its rows from a MYSQL database <table id=table1>
I erroneously delete all the rows from a MS SQL 2000 table that is
I want to delete all rows in a datatable. I use something like this:
I am using oracle database.I want to use duplicate rows from a table except
I want to delete all rows in a table with a specific ID (not
I want to delete all subdirectories and files from a directory, but not the
I want to delete all the elements from my list: foreach (Session session in
I want to delete all predicates named a from the list. The result must
I have a html document and I want to delete all the divs of

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.