Results 1 to 5 of 5
  1. #1
    vetabz is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jan 2017
    Location
    Philippines
    Posts
    13

    UNION Statement Allowed Duplicate in my Primary Key

    Hi,



    I have a Union statement combing two tables with similar fields - this should eliminate duplicate row and it did. However, I see duplicate numbers in my Primary Keys... How should I prevent that?

    I simply did a
    Code:
    Select * From Table1 UNION Select * From Table2.

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    A union query is not a table, so table rules, such as Primary Keys, do not apply.
    Is every single field a duplicate?
    If not, what is the logic for which record to keep and which one to drop?

    What exactly are you trying to accomplish?
    It might be helpful if you could post some samples of data from each table and your expected results.

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    to add to JoeM: the short answer is you cannot if you use the * symbol - - you would have to not include the Primary Key field in the query so instead of * as a short cut for all, instead specify each field and don't include the Primary Key field.

  4. #4
    vetabz is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jan 2017
    Location
    Philippines
    Posts
    13
    Thanks for clearing that out "A union query is not a table, so table rules, such as Primary Keys, do not apply."

    I'm pretty much contended with that statement alone.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you are just trying to combine the two tables into one, you can do an unmatched query to find all the records from Table2 that are not in Table1, then do an Append Query of those records to write them to Table1.

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

Similar Threads

  1. Replies: 2
    Last Post: 10-16-2015, 10:15 PM
  2. Replies: 3
    Last Post: 09-21-2015, 08:10 PM
  3. Sort Date in SQL UNION statement
    By KWarzala in forum Forms
    Replies: 5
    Last Post: 08-08-2014, 08:42 AM
  4. Suming Duplicate Records in Union Query
    By Sqnwk in forum Access
    Replies: 1
    Last Post: 10-30-2012, 06:10 PM
  5. UNION two tables with same primary key values
    By carillonator in forum Queries
    Replies: 1
    Last Post: 02-02-2010, 08:54 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