Results 1 to 5 of 5
  1. #1
    SteveI is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    6

    How do I set a primary Key when making a table with a Query?

    Hello all, thanks for looking at my question. I am really quite new to Access (as you will probably tell).



    I have created a table with a Make Table query which used a UNION query as the inputs.

    The Table created has no primary key set and I wanted to set the primary key as the field 'Product' as this matches with another feld in a different table which I will use later. I would prefer it if this could be done as part of a query as I will carrying out this everyday on many tables and intend to make it into a Macro.

    If anyone can tell me how to do this I would be greatly appreciative.

    Thanks

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    To assign a field as the primary key in an exsisting table you can run the following sql from a form a Commandbutton on a form using either Docmd.RunSql or Execute method.

    assumptions:
    Table Name: Persons (Type your table name)
    Primary Key Fields: P_id, LastName (In your case it will be products)


    ALTER TABLE Persons
    ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)


    Note:
    If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).


    if his solves you problemmark the thread to be solved.

  3. #3
    SteveI is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    6
    Thanks Maximus, this does work for the table in question. Here is the Sql to create the table, is it possble to put the ALTER TABLE into the CREATE TABLE Query rather than havng two seperate queries. I only ask because I am repeating this on lots of tables.

    SELECT UNIONQueryPrices.* INTO NewTable
    FROM UNIONQueryPrices;

    If so please can you tell me where I can put the ALTER TABLE statement and the syntax I need to use (I said I was new to Access).

    Don't know if makes a difference but there are two fields in the table Product and Price.

    Or alternatively is it possible to make one query that updates several tables with the Primary key all on the field Product?

    I really appreciate your help.

  4. #4
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Can you give me your table structure. i may have a solution that may be useful. cannot suggest until i get your table structure. alter and create table cannot be used together. But the Idea that i have is to create a table with all the required fields set the primary key and then Insert the values from the union query. plz send me the table structure.

  5. #5
    SteveI is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    6
    Not sure what you measn by Table Structure so I have attached the whole database.

    The tables I wish to merge together are UNIONFirsttoTable, UNIONSecondtoTable, UNIONThirdtoTable and UNIONFourthtoTable all into the same format as the Grades table.

    Thanks

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

Similar Threads

  1. Replies: 7
    Last Post: 01-07-2010, 12:20 PM
  2. making into update query
    By tom4038 in forum Queries
    Replies: 1
    Last Post: 09-23-2009, 11:19 AM
  3. Replies: 1
    Last Post: 06-01-2009, 01:09 PM
  4. Making a new field in a query
    By mslieder in forum Queries
    Replies: 3
    Last Post: 05-14-2008, 11:44 AM
  5. Making a backup copy of table or database
    By wasim_sono in forum Access
    Replies: 0
    Last Post: 03-09-2006, 05:44 AM

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