Results 1 to 8 of 8
  1. #1
    CGadda is offline Novice
    Windows 10 Office 365
    Join Date
    Dec 2024
    Posts
    1

    How do I prevent duplicate entries in many to many relationships?

    Got a problem that my introductory class in databases did not prepare me for. I have some many to many relationships that I have sort of resolved via associative entity tables. However, I have an issue where I get duplicative outputs in my queries.



    The database I'm working on is intended to track hardware and software service level costs for a given computer S/N. It is possible to have multiple hardware and software services assigned to a single S/N. The problem I'm finding is how to avoid duplicative costs. in the attached examples I have (for this particular set of two devices) a single HW service cost and multiple SW service costs. While the SW costs are fine, Access insists on filling in the rows with the same HW cost to match with each SW cost, even though there is only ONE HW cost (obviously, when I try to show totals, my results in this example are all wrong because the HW cost is now tripled!)

    Looks like I cannot insert attachments inline but they should be at the end of the post. The first screenshot shows the current output. The second is a simplified depiction of the portion of the database's relationship diagram, and the third is sort of an idealized "what I should see" as a query output. I'm hoping you all can provide tips as to how to fix this problem. Thanks!
    Attached Thumbnails Attached Thumbnails QUERY Example Output.png   Simplified Cost Relationships.jpg   QUERY Example Output - DESIRED.png  

  2. #2
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    564
    Don't totally understand the Totals tables, but anyway. (normally, that would just be a simple totals query).

    The standard structure for a problem like this is something like this. (Say you have a set of license keys that you can reuse some number of times).

    Computer(HWSerialNo (Primary Key), {other columns})

    ComputerSoftware (HWSerialNo (primary key 1), SoftwareKey (primary key 2), InstallDate )

    Software (SoftwareKey (primary key), software title, LicenseCost, # of installs covered)

    then this is easy. If unit cost is related to Software, then this is crazy simple

    Oh. "Preventing Dupllicates". "Reading is Fundamental!" Sorry, forgot that bit. You create a unique index on the combination of (HWSerialNo and SoftwareKey) in the ComputerSoftware table. (I think you CTRL-click or Shift-click the two columns and then you click the Primary Key button. And that will ensure that you can't add more than one license for a given piece of software more than once for each Computer.

    SELECT cs.HWSerialNo, cs.SoftwareKey, cs.InstallDate, s.LicenseCost
    FROM ComputerSoftware cs INNER JOIN Software s ON cs.SoftwareKey = s.SoftwareKey
    INNER JOIN Computer c ON c.HWSerialNo = cs.HWSerialNo

    then you can base counts etc on that query.

  3. #3
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,818
    Hi
    Are you able to upload a copy of your database?

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Based on my interpretation of the relationships, it is wrong. I take it that "bridge" tables are meant to be junction tables. You're linking PK fields in junction tables to parent tables rather than pk in parent to fk in child, then between junction and parent/child. In one or more cases you have pk fields that are not joined to anything.

    Aside from the relationships, did they not teach you to avoid using special characters in object names (save for underscore _ ) and to not store calculations in 99.9% of cases? They should have. Note that you cannot post db copies by cut and paste. You must follow the same process as how you got the images in your post (unless things have changed here). So if you meant to provide a db copy, most of the time it must be zipped and should be compacted first.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    That's the way query should work. Getting the second output in a query is difficult. Better to build a report. Use its Sorting & Grouping features and textbox "Hide Duplicates" property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Ooops, my suggestion will not fix the totals issue.

    Maybe should be a report/subreport structure.

    Really would help to have your data for analysis.

    Get rid of those dollar signs from names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    564
    primary key of the junction table is wrong. It should be the combination of the two primary keys of the related tables.

    The combination of (SystemCostUID, HardwareCostUID) in the junction table should be unique. (click on both, add primary key)

    Then when you try to enter a duplicate, the table validation rules will reject the insert. (Generally speaking, do that for conditions that must ALWAYS be true.... and foreign keys is one of those... otherwise you deal with it in code... which in Access is a PITA).

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Doesn't have to designated as compound key, can just be compound unique index.

    And duplicate pairs in table is not the issue asked about. It's about repetitive data in query joining tables.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-05-2019, 09:26 AM
  2. Prevent Duplicate Entries
    By Abhijeet in forum Forms
    Replies: 2
    Last Post: 08-23-2015, 12:15 AM
  3. Prevent duplicate entries in linked table
    By arothacker in forum Forms
    Replies: 5
    Last Post: 02-11-2014, 11:08 PM
  4. Replies: 19
    Last Post: 04-05-2013, 01:28 PM
  5. Problem with code to prevent duplicate entries
    By ResearchRN in forum Programming
    Replies: 5
    Last Post: 11-08-2011, 12:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums