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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:32:35+00:00 2026-06-06T14:32:35+00:00

Full Question: From a child table linked to a row in its grandparent table

  • 0

Full Question:

From a child table linked to a row in its grandparent table by a foreign key (references grandparent primary key), is it possible to use a field from the referenced grandparent row in the child table’s composite foreign key?

My current database design is has a main parent table, Projects. Projects has two child tables, JobTitles and Tasks. Tasks then has a child table Subtasks. Subtasks then has a child table Assignments, which is a x-ref table putting employees on Subtasks. The problem I’ve come up to is assigning JobTitles to Assignments; JobTitles belong to a Project, and as such the Assignments under any given Project should only be able to reference JobTitles that share a Project. I recently asked this question regarding limiting choice of JobTitle to those sharing a project. However, since then I’ve found a composite foreign key to be a much cleaner solution.

Simple database layout:

  • Projects
    • JobTitles
    • Tasks
      • Subtasks
        • Assignments

I’ve found how to create a composite key here, but for this I would need to use the Project’s primary key as part of the composite foreign key.

Tables:

  • Projects
    • ProjectName (PK)
    • ProjectID (Unique Index)
  • JobTitles
    • JobTitle
    • ProjectID (Foreign Key -> Projects.ProjectID) (Composite PK: JobTitle-ProjectID)
    • JobTitleID (Unique Index)
  • Tasks
    • TaskName
    • ProjectID (Foreign Key -> Projects.ProjectID) (Composite PK: TaskName-ProjectID)
    • TaskID (Unique Index)
  • Subtasks
    • SubtaskName
    • TaskID(Foreign Key -> Tasks.TaskID) (Composite PK: SubtaskName-TaskID)
    • SubtaskID (Unique Index)
  • Assignments
    • EmployeeID (Foreign Key)
    • SubTaskID (Foreign Key -> Subtasks.SubtaskID) (Composite PK: EmployeeID-SubtaskID)
    • JobTitleID (Foreign Key -> JobTitles.JobTitleID)
    • AssignmentID (Unique Index)

In order to assign a JobTitle to an Assignment, I want to set up a composite foreign key using an Assignment’s ProjectID (from its parent Task) and its selected JobTitleID. The only problem is that I have no idea how to grab the ProjectID, which is two generations away, for use in a key. It could be possible to pass the ProjectID down through each generation by wrapping it in a composite key within each table, but composite keys aren’t something something to throw around willy nilly considering the toll they take on performance (not to mention passing the value down seems a bit sloppy). Is there any way to get to the ProjectID for use in the key without passing it through the other tables?

  • 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-06T14:32:36+00:00Added an answer on June 6, 2026 at 2:32 pm

    As you say, you can pass the ProjectID down through each table. I don’t think that’s sloppy, and it enables you to create composite primary keys that enable reuse of e.g. TaskIDs across different projects. Once one realises that, one observes that the common practice of using ID columns in every table is a little superfluous. One can instead use semantically meaningful data as keys, which is usually my preference (sure it’s more costly in terms of space, but the indexing results in relatively little impact on time):

    CREATE TABLE Projects (
      ProjectName VARCHAR(20) NOT NULL PRIMARY KEY
    );
    
    CREATE TABLE JobTitles (
      ProjectName VARCHAR(20) NOT NULL,
      JobTitle    VARCHAR(20) NOT NULL,
      PRIMARY KEY (ProjectName, JobTitle),
      FOREIGN KEY (ProjectName) REFERENCES Projects (ProjectName)
    );
    
    CREATE TABLE Tasks (
      ProjectName VARCHAR(20) NOT NULL,
      TaskName    VARCHAR(20) NOT NULL,
      ParentTask  VARCHAR(20),
      PRIMARY KEY (ProjectName, TaskName),
      FOREIGN KEY (ProjectName) REFERENCES Projects (ProjectName),
      FOREIGN KEY (ProjectName, ParentTask) REFERENCES Tasks (ProjectName, TaskName)
    );
    
    CREATE TABLE Assignments (
      ProjectName VARCHAR(20)  NOT NULL,
      TaskName    VARCHAR(20)  NOT NULL,
      JobTitle    VARCHAR(20)  NOT NULL,
      Email       VARCHAR(255) NOT NULL,
      PRIMARY KEY (ProjectName, TaskName, JobTitle),
      FOREIGN KEY (ProjectName) REFERENCES Projects (ProjectName),
      FOREIGN KEY (ProjectName, TaskName) REFERENCES Tasks (ProjectName, TaskName),
      FOREIGN KEY (ProjectName, JobTitle) REFERENCES JobTitles (ProjectName, JobTitle),
      FOREIGN KEY (Email) REFERENCES Employees (Email)
    );
    

    Since MySQL doesn’t support more powerful constraint validation, the only other option I can think of would be to define BEFORE INSERT and BEFORE UPDATE triggers on Assignments that raise errors to reject data that contains an invalid JobTitle. However, then you also would need to create triggers on JobTitles to handle situations where such referenced records change or are deleted; and then also on all the other tables that could give rise to a break in linkage. Ugly ugly ugly.

    Hence my preference is with the first approach given above.

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

Sidebar

Related Questions

Summary Below is the full question (a bit complicated in its full form) here's
The full question from our sample exam paper: Explain by highlighting the relevant parts
This is the edited question with full problem. Following is the table structure. (Only
Coming from another question of mine where I learnt not to EVER use db
I need to get the full path from a PID. I've checked this question
This is based on my full question . I decided to take it in
Here's some code (full program follows later in the question): template <typename T> T
Not a full-time jQuery nor Javascript dev so sorry if dumb question. I think
Sorry for this long post. The question is however small but requires full detail.
I have the following code, thanks to another SO question/answer: page = agent.page.search(table tbody

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.