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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:44:21+00:00 2026-05-26T06:44:21+00:00

This is likely a much broader SQL topic than Entity Framework, and I’m very

  • 0

This is likely a much broader SQL topic than Entity Framework, and I’m very much a newbie in both these arenas, but I’ll ask it in terms of Entity Framework.

I would like to enforce a many-to-8 relationship. My setup is this:

  • A PersonGroup needs 8 (unique) Persons.
  • A Person can be in many different PersonGroups.
  • The order of the PersonGroup matters (the first needs to remain first, etc).
  • Easy access to all people in a PersonGroup and all PersonGroups a Person is in.

I’ve tried the following:

1) Add 8 1..many associations between Person and PersonGroup. I can certainly not have more than 8 Persons per group using this solution. However, to find all groups a person is in I need to iterate over 8 variables in the Person field, which is clunky.

2) Add 8 ids to PersonGroup that match up with a Person. Once again, I can guarantee only 8 persons per group, but there is no automatic link back through the association of Person->PersonGroup. I now need to be sure to add it to two places.

3) Just do a many…many relationship and handle it in code. There are two problems with this: I cannot guarantee only 8 persons per group, and I’m unsure if I can assure the order remains the same.

So, which is the best, or what solution am I missing?

  • 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-26T06:44:22+00:00Added an answer on May 26, 2026 at 6:44 am

    An n:m relationship with a “catch”:

    Person
    ------
    PersonId
    PRIMARY KEY (PersonId)
    
    
    PersonGroup
    -----------
    GroupId
    PRIMARY KEY (GroupId)
    
    
    Belongs
    -------
    GroupId
    PersonId
    Ordering
    
    PRIMARY KEY (GroupId, PersonId)
    FOREIGN KEY (GroupId)
      REFERENCES PersonGroup (GroupId)
    FOREIGN KEY (PersonId)
      REFERENCES Person (PersonId)               --- all normal up to here
    
    UNIQUE KEY (GroupId, Ordering)               --- the "catch"
    CONSTRAINT Ordering_chk                      --- ensuring only up to 8 persons 
      CHECK Ordering IN (1,2,3,4,5,6,7,8)        --- per group
    

    You should make sure that the CHECK constraint is available in the SQL engine you’ll use (MySQL for example would trick you into believing it has such constraints but it simply ignores them. SQL-Server does not return an error but happily adds a NULL in the checked column if you try to insert one.)

    There is a limitation to this approach. The Ordering field has to be NOT NULL because if it is NULL, more than 8 rows (with NULL there) could be inserted (except for SQL-Server which would allow you only up to 9 rows, eight with values and one with NULL.)

    To ensure maximum of 8 rows and NULLs in the Ordering, you could make a more complex constraint like the one described in MSDN site, CHECK Constraints (if your RDBMS has such feature) but I’m not at all sure on the performance of such a beast:

    CREATE FUNCTION CheckMax8PersonPerGroup()
    RETURNS int
    AS 
    BEGIN
       DECLARE @retval int
       SELECT @retval = CASE WHEN EXISTS
                                  ( SELECT * 
                                    FROM Belongs
                                    GROUP BY GroupId
                                    HAVING COUNT(*) > 8
                                  )
                             THEN 0
                             ELSE 1
                        END 
       RETURN @retval
    END;
    GO
    ALTER TABLE Belongs
    ADD CONSTRAINT Ordering_chk 
          CHECK (CheckMax8PersonPerGroup() = 1 );
    GO
    

    The constraint could alternatively be created as a FOREIGN KEY to a reference table with 8 rows. (If you use MySQL, that’s the only way to have the equivalent of CHECK.)


    A variation would be to use the (GroupId, Ordering) as the Primary Key and not have any constraint on the (GroupId, PersonId) combination. This would allow for a Person having multiple positions in a Group (but still up to 8) .

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

Sidebar

Related Questions

I've been stuck on this likely very simple problem, but haven't gotten anywhere with
This question is very much related to: A) How to separate CUDA code into
This likely applies to non MVC too. But, Is it possible to use the
This is likely going to be an easy answer and I'm just missing something,
This is likely by design for big teams with proper QA departments, but we
This is likely a stupid question but I always find myself wondering which is
I think this is likely to be a generic .NET assembly loading question, but
This is most likely not an easy one but here is the situation: I
I know this is most-likely a simple question but when you restore a database
I realize that the answer to this question is likely quite obvious (if somewhat

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.