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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:38:12+00:00 2026-05-20T05:38:12+00:00

I have two tables (MS SQL Server 2005) with an existing application (no DB

  • 0

I have two tables (MS SQL Server 2005) with an existing application (no DB alterations other than indexes, etc are allowed).

The two tables are:

ActivityDetails (Primary table)
    ActivityDetailkey (Primary key)

SubActivities (Child table)
    ActivityDetailKey (Refers to ActivityDetails)

Now, there are no contraints on SubActivities for the ActivityDetailKey. Basically, for EACH ActivityDetail row, there can be MANY SubActivities rows and there is nothing stopping the user from deleting an ActivityDetails row and leaving the SubActivities orphaned.

There was supposed to be some “soft locks” in the application that would prevent this but it isn’t working and I wanted to put some better integrity in the DB layer too.

But I can’t seem to add the foreign key because the SubActivities‘s ActivityDetailKey column isn’t UNIQUE.

How can I prevent people from deleting the ActivityDetails rows if there are children present?

Thanks

EDIT

I apologize for the complexity. The SubActivities table is actually named TEDetailSubActivities. I changed it in the question so that it would be easier to read.

But anyway, here is a gist of the complete schema for both tables.

https://gist.github.com/840479

I appreciate any help.

  • 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-20T05:38:12+00:00Added an answer on May 20, 2026 at 5:38 am

    It sounds like you’re trying to set up your foreign key the wrong way around – if there are multiple rows in SubActivities with the same ActivityDetailKey value, and these are references to the primary key in ActivityDetails, then the following should work (based on your posted schema, and now tested):

    ALTER TABLE TEDetailSubActivities ADD CONSTRAINT FK_TEDetailSubActivities_ActivityDetails FOREIGN KEY
          (ActivityDetailKey) references dbo.ActivityDetails (ActivityDetailKey)
    

    previous version, based on table names in post:

    ALTER TABLE SubActivities ADD CONSTRAINT FK_SubActivities_ActivityDetails FOREIGN KEY
          (ActivityDetailKey) references ActivityDetails (ActivityDetailKey)
    

    There’s no uniqueness requirement on the ActivityDetailKey column in SubActivities.

    As-is, that’ll stop deletion of rows from ActivityDetails if there are rows in SubActivities that reference them. If, on the other hand, you want the application to be able to continue with its deletes, but avoid leaving orphaned rows in SubActivities, add ON DELETE CASCADE after the final closing bracket above.


    The above works based on the following table definitions. If it doesn’t work in your database, you need to help us out by posting either the actual table definitions from your database, or something “close enough” for us to mimic what you’re seeing:

    create table ActivityDetails (
        ActivityDetailkey int not null Primary key
    )
    go
    create table SubActivities (
        ActivityDetailKey int not null
    )
    go
    

    Sigh. If you’re going to insist on using the SSMS designers:

    • Right click on SubActivities, choose “Design”.
    • Press the “Relationships” toolbar button
    • Press “Add”
    • Press “…” against the “Tables and Columns Specification” property
    • In the “Primary key table” drop down, choose “ActivityDetails”
    • In the grid below, choose ActivityDetailKey on both sides
    • Press “OK”, “Close”, the “Save” toolbar button, and (if necessary) “Yes” to the save warning
    • Close the Designer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have two tables in a SQL Server 2005 database, A and B.There is
I have got two tables in SQL Server 2005: USER Table: information about user
I'm wondering if this is possible in SQL. Say you have two tables A
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, both with start time and end time fields. I need
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables, one that contains volunteers, and one that contains venues. Volunteers

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.