6

I am editing a schema in MySQL workbench. I imported the schema from a mysqldump. Since it doesn't include any foreign keys, I am adding the relationships manually.

Here is my problem: When adding a relationship, workbench adds a column with the foreign key to one of the tables. However, the column is already present. I have to remove the current column and change the name of the new column to the name of the original column.

I illustrated it in the attached pictures:

  1. The two original tables
  2. The two tables after adding the relationship
  3. After removing original column and renaming new column - this is what I want in one step

The two original tables The two tables after adding the relationship After removing original column and renaming new column - this is what I want in one step

Is there a way to avoid this, and choose the target-column when adding the relationship, so that I don't have to edit a larger number of tables?

(Edited to add: The example tables are not the tables I want to connect, in case somebody recognizes the Drupal 8 tables - they were just short enough to aid as an example).

2
  • 1
    Use SQL commands instead of WorkBench: ALTER TABLE table_name1 ADD CONSTRAINT fk_name FOREIGN KEY (column_name) REFERENCES another_table_name( column_name ); Commented Oct 19, 2013 at 20:16
  • Thanks! I found a the form to add foreign keys to a table within workbench, works like a charm, relationships instantly displayed. I wanted to avoid having to re-import the schema, still figuring out the relationships. Commented Oct 19, 2013 at 21:15

1 Answer 1

12

Most of the relationship commands automatically create columns to host the new relationship. However, there's one tool that allows to select existing columns for a 1:n relationship. See in the tools toolbar. The last icon contains a picker symbol. This is the one to use. For quick access simply press '6'.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Mega thanks. Funny thing the icon is right there but didn't care to read 😅

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.