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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:15:19+00:00 2026-05-25T03:15:19+00:00

There are 3 cases 1) Basic Sender —> Receiver 2) Parallel Sender —-> Receiver1

  • 0

There are 3 cases

1) Basic
Sender ---> Receiver

2) Parallel
Sender ----> Receiver1
       ----> ReceiverN

3) Chained
Sender ----> Primary Receiver  -----> Secondary Receiver1
                               -----> Secondary ReceiverN

For 1) Basic and 2) Parallels, you would probably design your tables like this

Account
-Id (PK)
-UserId (FK)
-Name
-Description
-etc

Entry
-Id (PK)
-SenderAccountId (FK)
-ReceiverAccountId (FK)

Now how would you design the database to record “Chained” entries?

  • 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-25T03:15:20+00:00Added an answer on May 25, 2026 at 3:15 am

    You can add a many to many relationship between Account(Id-PK,UserId-FK,Name,Description,…) and Entry(Id-PK) tables:
    EntryAccount(EntryId & AccountId-PK,EntryAccountType)
    where EntryAccountType field can have one of the following values {S=Sender,R=Receiver, P=Primary receiver,N=secoNdary receiver}.

    The INSERT statements for EntryAccount table will be:

    --Basic
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'S')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'R')
    
    --Parallel
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'S')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'R') 
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'R')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'R')
    
    --Chained
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'S')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'P')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'N') 
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'N')
    INSERT EntryAccount (EntryId,AccountId,EntryAccountType)
    VALUES (...,...,'N')
    

    Then, to enforce some of business rules (one sender-S, one primary receiver-P and many [secondary] receivers-R/N) you can create an unique filtered index(SQL Server 2008) on EntryAccount table: IUF_EntryAccount_EntryId_EntryAccountType(key > EntryId & EntryAccountType, filter > EntryAccountType IN (‘S’,’P’)). Also, this index is good for query optimization.
    But, this index is not enough because you can have “inconsistent” Entry business objects like these:

    Entry(1001)
    EntryAccoount(1001,...,'S') without EntryAccoount(1001,...,'R') 
    or
    EntryAccoount(1001,...,'R') without EntryAccoount(1001,...,'S')
    , etc.
    

    To correct this problem you need a trigger AFTER INSERT, UPDATE, DELETE on EntryAccount table:

        CREATE TRIGGER ...
        AFTER INSERT, UPDATE, DELETE
        ...
        DECLARE @Results TABLE
        (
        EntryId INT PRIMARY KEY
        ,SendersCount INT NOT NULL DEFAULT O
        ,ReceiversCount INT NOT NULL DEFAULT O
        ,PrimaryReceiversCount INT NOT NULL DEFAULT O
        ,SecondaryReceiversCount INT NOT NULL DEFAULT O
        );
        INSERT @Results(EntryId)
        SELECT EntryId
        FROM inserted
        UNION --no duplicates
        SELECT EntryId
        FROM deleted;
    
        --Count senders
        UPDATE @Results
        SET SendersCount = q.Num
        FROM @Results r
        JOIN
        (
        SELECT ea.EntryId, COUNT(*) Num
        FROM EntryAccount ea
        JOIN @Results i ON ea.EntryId = i.EntryId
        WHERE ea.EntryAccountType = 'S'
        GROUP BY ea.EntryId
        ) q ON r.EntryId = q.EntryId;
    
        -Count [standard-R] receivers
        UPDATE @Results
        SET ReceiversCount = q.Num
        FROM @Results r
        JOIN
        (
        SELECT ea.EntryId, COUNT(*) Num
        FROM EntryAccount ea
        JOIN @Results i ON ea.EntryId = i.EntryId
        WHERE ea.EntryAccountType = 'R'
        GROUP BY ea.EntryId
        ) q ON r.EntryId = q.EntryId;
    
        --Count primary-P receivers
        UPDATE @Results
        SET PrimaryReceiversCount = q.Num
        FROM @Results r
        JOIN
        (
        SELECT ea.EntryId, COUNT(*) Num
        FROM EntryAccount ea
        JOIN @Results i ON ea.EntryId = i.EntryId
        WHERE ea.EntryAccountType = 'P'
        GROUP BY ea.EntryId
        ) q ON r.EntryId = q.EntryId;
    
    
        --Count secondary-N receivers
        UPDATE @Results
        SET SecondaryReceiversCount = q.Num
        FROM @Results r
        JOIN
        (
        SELECT ea.EntryId, COUNT(*) Num
        FROM EntryAccount ea
        JOIN @Results i ON ea.EntryId = i.EntryId
        WHERE ea.EntryAccountType = 'N'
        GROUP BY ea.EntryId
        ) q ON r.EntryId = q.EntryId;
    
        --Final validation
        IF EXISTS
        (
        SELECT *
        FROM @Results r
        WHERE NOT(r.SendersCount=1 AND r.ReceiversCount>=1 AND r.PrimaryReceiver=0 AND r.SecondaryReceiversCount=0 
        OR r.SenderCount=1 AND r.ReceiversCount=0 AND r.PrimaryReceiver=1 AND r.SecondaryReceiversCount >=1
        OR r.SenderCount=0 AND r.ReceiversCount=0 AND r.PrimaryReceiver=0 AND r.SecondaryReceiversCount=0
    )
        )
        ROLLBACK;
    

    If you don’t have SQL Server 2008 (R1/R2) you cannot create filtered index but you can rely only on trigger.

    PS: I have not tested this solution.

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

Sidebar

Related Questions

I'm using sequential ids as primary keys and there are cases where I don't
When are custom Exception classes most-valuable? Are there cases when they should or should
Are there any cases in which anything more than a linear speed increase comes
Are there any cases when it's a good idea to throw errors that can
I have some HTML I am trying to parse. There are cases where the
Why do the Silerlight-generated WCF proxy class(es) offer only async calls? There are cases
What business cases are there for using Markov chains? I've seen the sort of
I know there are many cases which are good cases to use multi-thread in
First of all, there are many cases where Sleep() is misused , for example
In Python scripts, there are many cases where a keyboard interrupt (Ctrl-C) fails to

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.