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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:06:11+00:00 2026-05-25T11:06:11+00:00

May someone help me out. I’m trying something, but I’m (too) new to (my)SQL.

  • 0

May someone help me out. I’m trying something, but I’m (too) new to (my)SQL.

I use two tables: Items and Categories. Table Items has a field with foreign key: category_id.

I want the table Categories to be kept tidy. So when no item in Items is of category X in Categories, the category X should be deleted from Categories. How do you establish that. I guessed by using DELETE ON CASCADE, but so far it was only deleting corresponding items from Items when I deleted a categorie from Categories.

Thanks a lot for helping me!

  • 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-25T11:06:11+00:00Added an answer on May 25, 2026 at 11:06 am

    ON DELETE CASCADE is a way of deleting a row when a row it references is deleted. This means:

    • You have a row in table A
    • You have a row in table B that references a row in table A
    • You delete the row in table A
    • The database deletes the corresponding row in table B

    So you have items, and each item belongs to a particular category. In your items table, you have a category_id (and please fix your spelling) that refers to a row in the categories table. So, in your situation:

    • You have a category
    • You have an item that references a category
    • You delete a category
    • The database deletes all the items that correspond to that category

    What you’re asking for is sort of the other way around:

    • You have items
    • You delete the last item in a particular category
    • The database goes and finds that category and deletes it

    There’s no way to do this with ON DELETE CASCADE, for two reasons:

    1. How are you going to create an empty category before inserting your first item into it? The database would have to delete it immediately.
    2. The database would have to do a lot of extra work scanning the table. It doesn’t “know” that item #23082 was the last item in the category; it would have to somehow be keeping track of the number of items in the category to do that.

    This all stems from the fact that ON DELETE CASCADE is a way of maintaining referential integrity. That is, it’s a way for the database to give you a strong guarantee that if you see category #20393 on item #9847, when you go look for category #20393 you know it exists. It is not a labor saving device. 🙂 This is why the other options are ON DELETE SET NULL and ON DELETE RESTRICT: they also guarantee integrity, but instead of deleting, they remove the bad reference or prevent the original delete from occurring.

    So the answer is, you will have to either write a cron job to periodically clean that table or use some kind of ON DELETE trigger, if you’re worried about empty categories.

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

Sidebar

Related Questions

I wonder whether someone may be able to help me please. Firstly apologies as
I wonder whether someone may be able to help me please. I'm using the
I wonder whether someone may be able to help me please Firstly, my apologies
I wonder whether someone may be able to help me please. Firstly, this is
I wonder whether someone may be able to help me please. Firstly, my apologies
I wonder whether someone may be able to help me please. I feel I
I wonder whether someone may be able to help me please. Firstly my apologies.
I wonder whether someone may be able to help me please. I'm using Aurigma
I have a problem that I thought someone may be able to help me
I'm trying to figure out how to use the log or debug commands in

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.