Results 1 to 6 of 6
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277

    How to change primary keys without the need to break relationships?

    Hello everybody,

    I have a table ORDERS and ORDER DETAILS.



    The ORDERS table has primary key set as the internal ID of that order, for example 2017ABC0001 (year, supplier code and number). We need to change this to ORD2017ABC0001 and DEL2017ABC0001 (order and delivery) to distinguish accordingly.

    The problem is that I already have many ORDER DETAILS items assigned to the orders so it won't let me do that.

    Any work around this?

    Thanks very much

    Tomas

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The reason why most people use autonumber primary keys!

    You will have to recreate the tables:
    - make a copy of both tables and add this as a new field
    - run an update query on Orders table and change the primary key, putting the result in the new field
    - run an update query on Order Details table, join by the old PK and update the new field to the one on the Orders table
    - on both tables, remove the current primary key, change the new field to the primary key

    Test and check, test and check!

  3. #3
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    Make a backup copy of your database first.

    In Table Relationships, delete the link from the primary table and the child table.
    Change the primary key value in both tables as needed.
    Recreate the link in Table Relationships as it was previously, and save.

    It begs to ask a question, why you would need to do this - primary keys are not meant to change.

  4. #4
    Thomasso is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277
    I just wanted to make the change for the upcoming orders but I didn't know that I can actually change input mask and leave past orders intact. Nevermind then, I just changed the input mask and will use the new format from now on. I can live with the past orders to have the old format.

    Thanks anyway.

  5. #5
    Join Date
    Apr 2017
    Posts
    1,776
    Close all forms until ended;
    In Orders table, add a new field OrderIDNew;
    Run an Update query on Orders table, setting new ID values into OrderIDNew field;
    Run an Delete query on OrderDetails table, deleting all orphan entries (OrderID don't exist in Orders table);
    Run an Update query on OrderDetails table, setting OrderID values to corresponding OrderIDNew values from table Orders;
    Repeat last 2 steps for any tables having OrdeID as foreign key;
    Run an Update query on Orders table, setting IrderID = OrderIDNew;
    Delete the column OrderIDNew from Orders table.

    Edit. As first step, change the format of OrderID in all tables to match with length of new OrderID.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Tomas,

    In addition to all the advice you have received, remember that Access ( and relational databases generally) work very well with 1 fact in 1 field. I recommend you do not try to concoct codes when storing data. You can always concatenate atomic fields for presentation.
    Fix the root problem if this system is important; don't add workarounds to address symptoms.

    eg 2017ABC0001 (year, supplier code and number).

    OrderDate
    SupplierCode
    Order????


    Here is a link to info on database design and planning that may be useful.

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

Similar Threads

  1. Replies: 20
    Last Post: 01-09-2017, 12:08 AM
  2. Replies: 1
    Last Post: 01-27-2016, 04:30 PM
  3. Help allocating Primary/Foreign Keys and Relationships
    By rosscortb in forum Database Design
    Replies: 2
    Last Post: 07-08-2015, 08:12 AM
  4. Primary Keys & Relationships
    By Njliven in forum Programming
    Replies: 4
    Last Post: 12-17-2012, 09:42 AM
  5. Primary/foreign keys + relationships
    By tripptofer in forum Database Design
    Replies: 0
    Last Post: 08-12-2009, 07: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