Results 1 to 5 of 5
  1. #1
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118

    Problems creating a Foreign Key using MySQL workbench

    I have an Access DB that needs splitting and backend hosted on a VPN as MySQL tables. I had not previously used MySQL but have successfully exported all the tables to MySQL then linked back to Access using a ODBC connector. I originally thought that the Access Export tool would create all the relationships and all I would need to do is then link to them. However this was not the case, I have to first create all the Primary & Foreign Keys. Using the MySQL workbench I have successfully created Primary Keys and also the lookup table relationships for combo lists however when trying to create the Foreign keys for the 1 -Many relationships I keep getting errors. I have 50+ that need to be done but the below is an example of just one along with the MySQL script.

    Tbl_Emp_Details Tbl_Emp_Alert
    PK Emp_ID PK Emp_Alert_ID
    Emp_First_Name FK Emp_ID
    Emp_Last_Name

    Trying to create a one to many between PK Emp_ID & FK Emp_ID

    Code

    1. ALTER TABLE `TCTSandpit`.`tbl_Emp_Alerts`
    2. ADD CONSTRAINT `Emp_ID`
    3. FOREIGN KEY (`Emp_ID`)
    4. REFERENCES `TCTSandpit`.`tbl_Emp_Details` (`Emp_ID`)
    5. ON DELETE CASCADE
    6. ON UPDATE CASCADE;


    ERROR 1005: Can't create table `TCTSandpit`.`tbl_Emp_Alerts` (errno: 121 "Duplicate key on write or update")



    The code was created using the MySQL workbench GUI. Any help on how to create the relationship appreciated.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If the constraint forbids duplicate FK values, that's the problem.

  3. #3
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    Thanks Davegri,
    How can I check if the constraint does forbid duplicate keys? I saw the reference to duplicates in the error massage but don’t understand what it is telling me. The child table only contains one foreign key (Emp_ID) related to Emp_ID in the parent table. The data will only contain one value of a type e.g. if the employee has an Alert (Allergy, Phobia etc.) they may have one of each but never the same one twice. Emp_ID is used as a foreign key in other tables e.g. addresses, contact numbers, etc. but surly I don’t need to give it a different name in each table? The one to many relationship works fine in Access but MySQL will not allow me to create it in the workbench.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Quote Originally Posted by Ant_Snell View Post
    Thanks Davegri,
    How can I check if the constraint does forbid duplicate keys? I saw the reference to duplicates in the error massage but don’t understand what it is telling me. The child table only contains one foreign key (Emp_ID) related to Emp_ID in the parent table. The data will only contain one value of a type e.g. if the employee has an Alert (Allergy, Phobia etc.) they may have one of each but never the same one twice. Emp_ID is used as a foreign key in other tables e.g. addresses, contact numbers, etc. but surly I don’t need to give it a different name in each table? The one to many relationship works fine in Access but MySQL will not allow me to create it in the workbench.
    Don't know anything about workbench. Workbench documentation should explain what the parameters are for. Maybe just drop the ADD CONSTRAINT line?

  5. #5
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    1. ALTER TABLE `TCTSandpit`.`tbl_Emp_Alerts`
    2. ADD CONSTRAINT `Emp_ID`
    3. FOREIGN KEY (`Emp_ID`)
    4. REFERENCES `TCTSandpit`.`tbl_Emp_Details` (`Emp_ID`)
    5. ON DELETE CASCADE
    6. ON UPDATE CASCADE;

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

Similar Threads

  1. Replies: 6
    Last Post: 08-17-2020, 04:38 AM
  2. Problems creating form - first time use
    By albaker in forum Forms
    Replies: 6
    Last Post: 09-30-2019, 04:30 PM
  3. Print view MySQL Database Install problems!
    By spadekevin in forum Misc
    Replies: 1
    Last Post: 06-29-2018, 11:07 AM
  4. problems creating one to one relationship
    By alexthefourth in forum Access
    Replies: 5
    Last Post: 11-11-2013, 06:16 PM
  5. SEARCH problems with Access and MySql
    By azirion in forum Access
    Replies: 2
    Last Post: 08-27-2011, 05:02 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