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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:58:58+00:00 2026-06-13T14:58:58+00:00

I have several tables which reference one main table using foreign keys. My goal

  • 0

I have several tables which reference one main table using foreign keys.

My goal is for one blanket delete statement from the main table, which will then delete all those with the data as foreign keys as well.

However I get constraint errors as I expect since the reference is deleted before its deleted from the other tables. Is there any way to tell SQL I would like to delete from all tables where the data is used as a foreign key as well?

  • 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-13T14:58:58+00:00Added an answer on June 13, 2026 at 2:58 pm

    To have deletes on the parent table cascade to referencing tables, use an ON DELETE CASCADE foreign key. However, if you’re deleting all rows from the main table there’s a better way to solve your problem using TRUNCATE; see the end of this post.

    You can ALTER TABLE to change a default ON DELETE NO ACTION foreign key to ON DELETE CASCADE. Drop the constraint then re-create it with the ON DELETE CASCADE modifier. See CREATE TABLE and the constraints documentation for information on the foreign key constraint syntax.

    In brief, instead of col coltype REFERENCES fktable(fkcol) you use col coltype REFERENCES fktable(fkcol) ON DELETE CASCADE.

    If you use ON DELETE CASCADE then to get even vaguely decent performance it is vital to create an index on the foreign key column. Even then, for bulk deletes from the target table it’s often much faster to go and delete from each foreign key referencing table first, rather than relying on cascades.

    You may want to also use ON UPDATE CASCADE if you’re using ON DELETE CASCADE.


    If you’re deleting all rows from the main table, not just a selection of them, you’re probably better off truncating the tables with TRUNCATE ... CASCADE:

    CASCADE

    Automatically truncate all tables that have foreign-key references to
    any of the named tables, or to any tables added to the group due to
    CASCADE.

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

Sidebar

Related Questions

I have a table which has several one to many relationships with other tables.
I have a mysql database which have set of tables One table have a
I have a database with several tables, one being used to reference others :
I have working website in PHP with a MySQL backend which has several tables
Hi i'm trying to create a league table in which i have several different
We have a table T which contains a several char(8) columns (implicitly) which under
I have a table called scheduler_sched which has several columns, including a column called
I have a table with multiple indexes, several of which duplicate the same columns:
I have several tables, books, bookcategories, categories bookcategories is a join table to allow
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT

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.