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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:09:33+00:00 2026-06-03T03:09:33+00:00

I have a table Universities and a table Cities. There is a m:n relationship

  • 0

I have a table Universities and a table Cities. There is a m:n relationship between them, so the relationship is saved in a further table called CityUniversities. Now I wanted to write a trigger which deletes all relations between the tables Cities and Universities when deleting a university from Universities. However it’s not working. I always receive the following error message when trying to delete a University:

“Msg 547, Level 16, State 0, Line 2
The DELETE statement conflicted with the REFERENCE constraint “FK_CityUniversities_Universities”. The conflict occurred in database “Alumni_Dev”, table “dbo.CityUniversities”, column ‘UniversityId’.
The statement has been terminated.”

That’s the trigger (for table Universities) which I have implemented and succesfully executed:

USE [Alumni_Dev]
GO
/****** Object:  Trigger [dbo].[deleteUni]    Script Date: 05/02/2012 11:42:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[deleteUni]
ON  [dbo].[Universities] 
FOR DELETE
AS 
DECLARE @UniId int
SELECT @UniId = (SELECT UniversityId FROM deleted)
BEGIN
--Remove constraints
DELETE FROM dbo.CityUniversities WHERE UniversityId = @UniId;
END  

I already compared my solution with several references, however I can’t find anything that could be wrong. Could it be that there are any wrong settings in one of the affected tables?

I’d be happy to hear any suggestions.


I finally solved my problem with the following trigger (thx to Nikola):

ALTER TRIGGER [dbo].[deleteUni]
ON  [dbo].[Universities] 
INSTEAD OF DELETE
AS 
DECLARE @UniId int
SELECT @UniId = UniversityId FROM deleted
BEGIN
--Remove constraints
DELETE FROM dbo.CityUniversities WHERE UniversityId = @UniId;
DELETE FROM dbo.Universities WHERE UniversityId = @UniId;
END
  • 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-03T03:09:34+00:00Added an answer on June 3, 2026 at 3:09 am

    Trigger cannot be used to delete orphaned records from other tables because constraints are checked before trigger gets executed. You should set up ON DELETE CASCADE instead.

    You might use INSTEAD OF trigger also if you want to perform some checking before deletion, but don’t forget to add delete Universities where ... at the end of a trigger to actually delete record.

    Two points about triggers:

    • Add SET NOCOUNT ON at the beginning of trigger body to avoid posibility of sql statements inside trigger changing the value of “rows affected” which you might need in calling code,
    • Don’t treat deleted and inserted tables as if they contain one record only. There will be nasty surprises when you try to delete two universities. You should rather join a table you work with with deleted or inserted tables, using them as filter, or use exists:

      delete dbo.CityUniversities
        from dbo.CityUniversities
       inner join deleted
          on dbo.CityUniversities.UniversityId = deleted.UniversityId 
      

    or

    delete dbo.CityUniversities
     where exists (select null
                     from deleted
                    where deleted.UniversityId =dbo.CityUniversities.UniversityId)
    

    Oh, and

    SELECT @UniId = (SELECT UniversityId FROM deleted)
    

    is equivalent to:

    SELECT @UniId = UniversityId FROM deleted
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table called stats . In am inserting yes or no in the
I have table Owner in my database where one of the field (System) saved
I have table of over 4 million rows and accidentally in one column there
I have table of products, and there are 2 fields: price, currency. Possible values
I have table ,and i need access them from shell script and do the
I have table with pageId, parentPageId, title columns. Is there a way to return
i have table A and table B. I have a bridge table called tableC
i have table student,fields are student_id, student_name, year_joining ... now i want to rename
I have table Widgets and table Persons both of them contains Identity column (
I have table documents (id, name, time) . Is there a special sql command

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.