MySQL Workbench - adding relations without having to edit the tables - mysql

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:
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).

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'.

Related

How to make one to one relationship EER diagram in MySql Workbench 6.3E

As the title said , how can i make the relationship in following image (connect to column)?
In the vertical toolbar you can see different connection (relation) tools. The last one with the picker symbol lets you select individual columns for a relationship. You can edit the cardinality and other properties at any time in the relationship editor (double click the relationship to open it). By editing the referencing table you can also edit the participating columns (via the created foreign key constraint.
Note: If you don't have primary key in your table it won't work as expected.

How to use "central columns" in phpmyadmin?

PMA has the tools for adding the central columns. As far as I understand, it's used with foreign constraints. I have two tables: TableA and TableB.
Structure of TableA: id_of_A, name_of_A_value, ...
Structure of TableB: id_of_B, foreign_id_of_A,... and foreign constraint from foreign_id_of_A to A-table.id_of_A.
And it's very difficult to select needed foreign_id_of_A while insert new row into TableB because only value of id_of_A is visible. Could central columns help me with this problem? How central column works?
Central Column
You can add/remove columns to the list as per your requirement. These columns in the central list will be available to use while you create a new column for a table or create a table itself. You can select a column from central list while creating a new column, it will save you from writing the same column definition over again or from writing different names for similar column.
To allow the usage of this functionality:
set up $cfg['Servers'][$i]['pmadb'] and the phpMyAdmin configuration
storage
put the table name in $cfg['Servers'][$i]['central_columns'] (e.g. pma__central_columns)
This feature can be disabled by setting the configuration to false.
Perhaps the feature that you are looking for is the "Display Column" under "Relation View" in the table structure view. This allows you to choose what column is shown in the PHPMyAdmin interface regardless of what is stored in the database foreign key constraint.
If the line is not shown than you need to enable the feature in PHPMyAdmin. See the FAQ Entry and relevant configuration option entries: $cfg["servers"][$i]["relation"] and $cfg["servers"][$i]["table_info"] (in the previous link, at the time of posting (I don't have enough reputation to post more than 2 links)).
Essentially you create a couple of tables in a database that PHPMyAdmin can use to store additional data and then tell it which tables these are. Once this is done PHPMyAdmin will let you choose which columns to display (on mouse over of the linked column, and while editing data). If you installed it on a Linux distribution via a package manager it should have already created these tables for you (or at least asked you if you wanted to), if it did not, re-installing the package may help. I don't have any experience with installing it on Windows or Mac OS X, but I would presume that there is some automated method of creating these tables for those platforms.
(On Debian and Debian-based Linux Distributions (such as Ubuntu), the extra database is configured with db-config-common. If it was not configured at install time, dpkg-reconfigure phpmyadmin will allow you to set up the database (please note that this might destroy some user/system preferences in PHPMyAdmin).)
I've found here is an exhaustive description:
As the name suggests, the Central columns feature enables to maintain a central list of columns per database to avoid similar name for the same data element and bring consistency of data type for the same data element. You can use the central list of columns to add an element to any table structure in that database which will save from writing similar column name and column definition.
To add a column to central list, go to table structure page, check the columns you want to include and then simply click on “Add to central columns”. If you want to add all unique columns from more than one table from a database then go to database structure page, check the tables you want to include and then select “Add columns to central list”.
To remove a column from central list, go to Table structure page, check the columns you want to remove and then simply click on “Remove from central columns”. If you want to remove all columns from more than one tables from a database then go to database structure page, check the tables you want to include and then select “Remove columns from central list”.
To view and manage the central list, select the database you want to manage central columns for then from the top menu click on “Central columns”. You will be taken to a page where you will have options to edit, delete or add new columns to central list.
http://lees.geo.msu.edu/phpmyadmin/doc/html/faq.html
May be it's useful for foreign constraints modelling, but absolutly useless in usage. I'm still looking for a solution for adding value into one table by the ID from another table so that it was understandable for people, not for computers. If you know how to do that, let me know.

Database table setup: Multiple tables that serve the same purpose?

I need to setup a MySQL database for a bugtracker, that's paired with a changelog.
Therefore I essentially have three tables: product, version, problem, problem_solution. The reason I splitted problems and their solutions is that I want to be able to provide my users with a set of possible solutions.
Now I want to add attachments to each of these tables and manage them via the database as well. There should be pictures, PDFs, ... for each product, version and possibly for each problem and solution.
Would I rather
Create 4 attachment-tables (product_attachments, version_attachments, ...), or
Create one attachment-table and create a column stating what it is for?
If latter, how should I do it? I want to reference to the specific ID of the product, version, problem or solution using a foreign key. Should I then just create 4 columns, each of them with a foreign key and decide whether it's an attachment for a product, a version, ... depending on which of these columns is not NULL? Wouldn't this make my queries unnecessarily complex?
I say create one table, have its primary key available, and create another table of EAV type for multi-to-multi relation between attachments and other entities, with "value" corresponding to attachment ID, "entity" to foreign ID and "attribute" to a value out of a fixed set of product, version, problem, solution in any form you like (1,2,3,4?). This way the attachments will be stored in a table of id, blob structure, maybe with corresponding count column storing the amount of links in the relation table, so that an orphaned attachment could be detected and removed with ease.

Updating existing lines in MySql and treating Duplicated Keys

I have a MySql database containing data about users of an application. This application is in production already, however improvements are added every day. The last improvement I've made changed the way data is collected and inserted into the database.
Just to be clearer, my database is composed of 5 tables containing user data and 1 table to relate all the tables, through foreign keys. These 5 foreign keys, together, form my Unique Index for this "Main Table" I have.
The issue is that one of these tables containing user data changed its format, and I want to remove all the data older than the modification I made on my application (just from this table, the other ones I need to keep untouched). However, this dataset has foreign keys in the main table, and I can't just drop these lines on the main table because the other informations I have are important. I tried to change the value of the foreign key for this table, in specific, but then, obviously, I have a problem related to duplicated indexes.
Reading on internet, I've found a solution to my problem using "Insert ... On duplicate key update ...", but i'm not inserting data, just updating it. I have an Idea about how to make a program on PHP to update my database, but is there another easier solution? Is it possible to avoid these problems using just MySql syntax?
might be worth looking at the below link
http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html

MySQL database merge of two databases

I set up two Wordpress blogs a while ago, both obviously having different databases. I've more recently merged these databases into one by changing the tables prefixes, therefore these two 'entities' have the same amount of tables and the same names (as they originate from a Wordpress install) but with different prefixes, i.e.:
Blog1_tabledata1
Blog1_tabledata2
Blog1_tabledata3
Blog1_tabledata4
Blog2_tabledata1
Blog2_tabledata2
Blog2_tabledata3
Blog2_tabledata4
I have now realised that I need to merge these two databases (where they're both using the same tables) so that they can be used in the same Wordpress instance (later separated by tags etc).
What would be the most simple way of doing this?
(Please note I am asking this from a MySQL standpoint - this is not a Wordpress question!)
If you absolutely are not looking for a wordpress solution, that means you are not looking at all domains. By this, I mean that you are not looking at what the data means. This could be a problem. but nevertheless:
figure out the foreign keys. If the tables are MyIsam instead of InnoDB, they will be implicit. Figure out what ID points to what field.
select from one DB and insert into the other. This mean you add the rows of one table to the equivalent of the target database. Auto-increment rows will be fine. But for foreign key (explicit AND implicit) fields here is were the trouble starts.
If you insert, say a user, the user gets a new ID -> You have to find the equivalent of the userid in the old db so you can insert the foreign keys with the right ID. this is tricky and without making this a wordpress-question there is no more help we can give you: just figure out what rows they should be :). it is database // domain specific. (with that I mean you can't just figure that out by looking at the fields, you must know some of what they mean)
If the db is correct, this should work, but I'm not sure if you get into trouble with duplicates (all should go on ID, and you fixed that in step 3 with unique and connected id's. but if your domain doesn't want two accounts, two pages or two whatevers (tags?) to have the same name, you still have a problem. But again, this is domain specific logic and you're specifically asking not to go there.