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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:44:22+00:00 2026-06-14T04:44:22+00:00

I have a data table with a primary key called OptDefID . When a

  • 0

I have a data table with a primary key called OptDefID. When a record in this table is deleted I need to go and delete all records from the Permissions table that have that OptDefID in the defID field (in Permissions). The tricky part for me is that the Permissions table does not have a primary key and holds lots of different kinds of permissions, and has a permissiontype field. I need to delete rows that have the OptDefID AND a permissiontype of OptDef.

Because I need to consider the permissiontype, I don’t believe a Foreign Key Constraint is appropriate here (or is it?).

I’ve also considered creating a trigger, but am unsure how to get the OptDefID passed into the trigger.

I can do this via the application itself, but I feel like this should be a database level solution.

What’s the best solution?

  • 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-14T04:44:23+00:00Added an answer on June 14, 2026 at 4:44 am

    Say I want to delete from Permissions where defID is 20 and permissiontype is ‘OptDef’. There may be another row in Permissions that has a defID of 20, but has a permissiontype of ‘Member’. That show should not be deleted because it pertains to Members and not Opt data.

    Storing table names in fields prevents foreign keys from working properly, as you have discovered.

    I recommend you fix the root problem and separate these two foreign keys, so each of them can be individually enforced. For example:

    CREATE TABLE Permissions (
        ...
        OptDefId int,
        MemberId int,
        FOREIGN KEY (OptDefId) REFERENCES OptDef ON DELETE CASCADE,
        FOREIGN KEY (MemberId) REFERENCES Members ON DELETE CASCADE,
        CHECK (
            (OptDefId IS NOT NULL AND MemberId IS NULL)
            OR (OptDefId IS NULL AND MemberId IS NOT NULL)
        )
    )
    

    The CHECK makes sure only one of the FKs is non-NULL and only non-NULL FKs are enforced.

    Alternatively, you could avoid changing your current design and enforce this “special” FK through a trigger, but declarative constraints should be preferred to triggers when possible – declarative constraints tend to leave less room for error and be more “self-documenting”.

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

Sidebar

Related Questions

I have data in two tables. The first table has a Primary Key called
I have a table called blocks with a primary key blockid . The data
I have a table with composite primary key. I am trying to load data
I have a datatable Table called Table1 with primary key PId and child table
I have a table with a primary key and row key column. The data
I have a database table with a primary key called PremiseID. Its MySQL column
I have a table called Animal . AnimalId is the primary key & I
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
I have a data.table object similar to this one library(data.table) c <- data.table(CO =

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.