Results 1 to 9 of 9
  1. #1
    niloufar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    24

    Can I set PK inside my union query?

    I have this query which the result will be inserted into a table with Primary key to enforce the integrity rule.
    I was wonder if there is a way to design this query the way to apply integrity rule PK for [StudentID] to my query without having another table TO make it Updateable?

    ""If you wonder why? becase my original exported files ( excel tables ) are a mess and they have too many problems and even my union query doesn't drop duplicates! ""


    Code:
    INSERT INTO tblStudent ( [Student ID], [Student Name] )
    SELECT DISTINCT [tblStdList].[STD ID] AS [Student ID], tblStudent.tblStdList.Name AS [Student Name]
    FROM (SELECT tblStdList.[STD ID], tblStdList.Name
    FROM tblStdList
    GROUP BY tblStdList.[STD ID], tblStdList.Name
    
    Union
    
    SELECT tblSkillSoft.Username, tblSkillSoft.[Student Name]
    FROM tblSkillSoft
    GROUP BY tblSkillSoft.Username, tblSkillSoft.[Student Name]
    
    )  AS tblStudent
    ORDER BY [tblStdList].[STD ID];

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Have you attempted this query? Does it actually work? I don't see how it can but then never tried to INSERT SELECT UNION. Username is synonymous with [Student ID]?

    [Student ID] is primary key field in tblStudent?

    The UNION query won't know that [Std ID] is going into a PK field. The INSERT will error if it attempts to save a duplicate PK.
    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.

  3. #3
    niloufar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    24
    Hi June7,

    This query works in access & I actually changed names just for privacy. Yes, [Student ID] in my new table is the primary key.

    This is my main query That I would like to assign PK to the result I just would like to do that to make it updatable so I don't have to manually update the tblStudent...

    SELECT tblStdList.[STD ID], tblStdList.Name
    FROM tblStdList
    GROUP BY tblStdList.[STD ID], tblStdList.Name

    Union

    SELECT tblSkillSoft.Username, tblSkillSoft.[Student Name]
    FROM tblSkillSoft
    GROUP BY tblSkillSoft.Username, tblSkillSoft.[Student Name]



    Quote Originally Posted by June7 View Post
    Have you attempted this query? Does it actually work? I don't see how it can but then never tried to INSERT SELECT UNION. Username is synonymous with [Student ID]?
    Quote Originally Posted by June7 View Post
    [Student ID] is primary key field in tblStudent?

    The UNION query won't know that [Std ID] is going into a PK field. The INSERT will error if it attempts to save a duplicate PK.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Are you saying the INSERT sql works?

    I don't know what you mean by 'make it updatable' and 'assign a PK'. UNION query records are not editable.

    Isn't the PK field [Student ID] populated with the [STD ID] values? What in the table has to be manually updated?

    What names are in the query that had to be changed for privacy? Aren't you just referencing fields?
    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.

  5. #5
    niloufar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    24
    I send you PM

    I kind of normalized my two tables because they both are taken from different part of company. They have many errors and datatype problems.
    So I created new tables for each, then I combine them with union query; however the union doesn't eliminate duplicates and I had to create another table with PK to enforce integrity rule and eliminate duplicate IDs.

    BUT,
    I have to take so many steps every time I need new reports unless. If it was just a query the result was update-able, but now I have to update 3 tables to get my final report. I hope I could explain....

    What I basically need is to create a union query and enforce PK to my result. can I do that?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I don't see any new info in the PM.

    A UNION query will not show duplicate records. A UNION ALL will allow duplicate records. So I don't know what you mean by 'doesn't eliminate duplicates'.

    I still don't understand what you mean by 'enforce PK'. A UNION query cannot create a unique identifier for each record, like an autonumber field in a table.
    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
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Even if it would be possible to generate such an ID, the resultset wouldn't be updateable. Access tracks a per field table source (allowing updateable joins in some cases) but doesn't track a per record source and therefor will never know what record came from what table. Therefor it will never know where to write updates or deletes to. On top of that it is not possible to add a primary key, or even a normal index to a query (as long as we don't talk about sql server based technics). Access wouldn't be able to enforce such unique indices or even hold them up to date. To reliable work with your input data you should have some procedure to fix the errors and datatype problems anyway. That should be the point where you merge the tables into one as you do it now. Question is: where does your requirement to make updates in the underling original tables come from? Do you need the originals updated somewhere else?

  8. #8
    niloufar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    24
    Thank you hapm,
    You answered my question. I was looking for some trick that I hoped to exist.
    Yes, My imported tables will be updated occasionally as the result I have to update at least two normalized tables for those updates. So, I Think the only way is just having Macro to do the step for me

    Thank you again all for being thoughtful



    Quote Originally Posted by hapm View Post
    Even if it would be possible to generate such an ID, the resultset wouldn't be updateable. Access tracks a per field table source (allowing updateable joins in some cases) but doesn't track a per record source and therefor will never know what record came from what table. Therefor it will never know where to write updates or deletes to. On top of that it is not possible to add a primary key, or even a normal index to a query (as long as we don't talk about sql server based technics). Access wouldn't be able to enforce such unique indices or even hold them up to date. To reliable work with your input data you should have some procedure to fix the errors and datatype problems anyway. That should be the point where you merge the tables into one as you do it now. Question is: where does your requirement to make updates in the underling original tables come from? Do you need the originals updated somewhere else?

  9. #9
    niloufar is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    24
    Thank you June7, I guess I was hoping for some tricks


    Quote Originally Posted by June7 View Post
    I don't see any new info in the PM.

    A UNION query will not show duplicate records. A UNION ALL will allow duplicate records. So I don't know what you mean by 'doesn't eliminate duplicates'.

    I still don't understand what you mean by 'enforce PK'. A UNION query cannot create a unique identifier for each record, like an autonumber field in a table.

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

Similar Threads

  1. Make Table Quesry Help
    By worldwidewall in forum Access
    Replies: 4
    Last Post: 02-13-2012, 06:41 PM
  2. Union & union all
    By jasonbarnes in forum Queries
    Replies: 4
    Last Post: 10-27-2011, 12:30 PM
  3. Union or better way.
    By kevin28 in forum Access
    Replies: 2
    Last Post: 09-06-2011, 02:42 PM
  4. Union Query Help
    By jo15765 in forum Queries
    Replies: 7
    Last Post: 01-06-2011, 05:46 PM
  5. Please help me out with union query
    By radicrains in forum Access
    Replies: 1
    Last Post: 10-29-2010, 01:48 AM

Tags for this Thread

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