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

  • Home
  • SEARCH
  • 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 4099660
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:27:47+00:00 2026-05-20T20:27:47+00:00

what is the recommended way to handle self-referencing foreignkey constraints in SQL-Server? Table-Model: fiData

  • 0

what is the recommended way to handle self-referencing foreignkey constraints in SQL-Server?

Table-Model:

enter image description here

fiData references a previous record in tabData. If i delete a record that is referenced by fiData, the database throws an exception:

“The DELETE statement conflicted with the SAME TABLE REFERENCE
constraint “FK_tabDataPrev_tabDataNext”. The conflict occurred in
database “MyDataBase”, table “dbo.tabData”, column ‘fiData'”

if Enforce Foreignkey Constraint is set to “Yes”.

I don’t need to cascade delete records that are referenced but i would need to set fiData=NULL where it’s referenced. My idea is to set Enforce Foreignkey Constraint to “No” and create a delete-trigger. Is this recommendable or are there better ways?

Thank you.

  • 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-20T20:27:48+00:00Added an answer on May 20, 2026 at 8:27 pm

    Unlike Andomar, I’d be happy using a trigger – but I wouldn’t remove the constraint checking. If you implement it as an instead of trigger, you can reset the other rows to null before performing the actual delete:

    CREATE TRIGGER T_tabData_D
    on tabData
    instead of delete
    as
        set nocount on
        update tabData set fiData = null where fiData in (select idData from deleted)
        delete from tabData where idData in (select idData from deleted)
    

    It’s short, it’s succinct, it wouldn’t be necessary if SQL Server could handle foreign key cascades to the same table (in other RDBMS’, you may be able to just specify ON DELETE SET NULL for the foreign key constraint, YMMV).

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

Sidebar

Related Questions

What is the recommended way to embed PDF in HTML? iFrame? Object? Embed? What
Is there a recommended way to bounce an asp.net application besides touching web.config from
Which is the preferred/recommended way of handling events in .NET: this.Load += new EventHandler(Form1_Load);
Given a data structure (e.g. a hash of hashes), what's the clean/recommended way to
Very similar to this question , except for Java. What is the recommended way
What is the most recommended/best way to stop multiple instances of a setTimeout function
I am designing a REST API and I wonder what the recommended way to
I am trying to figure out the best way to handle a derived class
Recommended by the ASP.NET team to use cache instead of session, we stopped using
Any recommended practices for cleaning up header spaghetti which is causing extremely slow compilation

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.