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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:39:46+00:00 2026-05-25T01:39:46+00:00

I have a database in a SQL Server 2008 database. My database model forms

  • 0

I have a database in a SQL Server 2008 database. My database model forms a diamond pattern with four tables. Those four tables are defined as follows:

Table1
- ID
- Name
- AddedBy

Table2
- ID
- Table1ID
- Name
- Type

Table3
- ID
- Table1ID
- Name

Table4
- ID
- Table2ID
- Table3ID
- Age

I am currently getting all of the Table1 records for a specific user by using the AddedBy field. This query looks like this:

SELECT
  * 
FROM
  [Table1] t1
WHERE
  t1.[AddedBy]=@someuser

Now, I need to get the Age value from the first Table4 record that is somehow associated with Table1. How do I do this? I keep getting confused with the query.

Thank you for any help you can provide!

  • 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-25T01:39:47+00:00Added an answer on May 25, 2026 at 1:39 am

    This is a classic illustration of how compound primary keys can allow you to more accurately express what you want your database to do.

    Given the model that you have, it appears that Table2 and Table3 are directly defined by Table1; that is, it doesn’t make sense to have a Table2 record without a parent Table1 record. Likewise, it looks like Table4 only makes sense when both Table2 and Table3 exist. If this is true, then Table2, Table3, and Table4 should have compound primary keys along these lines:

    Table2
    -------------
    Table1ID     -- consider renaming this in Table1 so that the same name is used 
                    throughout
    RecordNumber -- unique within a given Table1ID; this is only needed if one of 
                    your other two columns cannot serve as a unique value within 
                    Table1ID, which I'm guessing one of them can
    

    Then you’d do something similar for Table3. Then, for Table4, you’d have:

    Table4
    -------------
    Table1ID
    Table2RecordNumber
    Table3RecordNumber
    

    As the primary key, then set up two foreign keys, one to Table2 on (Table1ID, Table2RecordNumber) and one to Table3 on (Table1ID, Table3RecordNumber). This allows you to ensure that your Table4 records always link to Table2 and Table3 records with the same Table1ID, and it simplifies the join in the original query so that it doesn’t have to go through Table2 or Table3 to find a valid record in Table4.

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

Sidebar

Related Questions

I am creating a new SQL Server 2008 database. I have two two tables
Our database server is a SQL 2008 server. My colleagues all have XP service
I have designed database tables (normalised, on an MS SQL server) and created a
I have a SQL Server 2008 database table that uses uniqueidentifier as a primary
We have a database (SQL Server 2005) which we would like to get under
We have SQL Server database setup. We are setting up a replication scenarios where
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
I have an SQL server database that I am querying and I only want
We have a SQL server database. To manipulate the data non-programmatically, I can use
In a SQL server database, I have a table which contains a TEXT field

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.