Merging individual tables? - mysql

I have 5 tables. table1, table2, table3, table4 and table5.
All these tables are indexed the same way, which means, for example, for name john75de, table1 contains personal details
table2 contains his skill set, table3 contains the professional experience, table4 contains the qualifications, and table5 contains additional qualifications.
These tables have been there from the beginning, but I think I need to merge all of the data into one table because these 5 tables are all referenced using the same index. There are 500k names and details in all these tables.
There are no other cross-relations between names or any data. So what do you think. Do I need to merge it? If yes, how can I do it? Since all the data relating to one name will be in a single row, I hope this will be easier. Oh, yeah, names are unique.

It sounds like a bad idea to merge these tables. A person can have multiple addresses, multiple skills, multiple previous jobs and multiple qualifications. To model this you need one to many relationships, which is best done using separate tables.
If "qualifications" and "additional qualifications" have the same structure I might be tempted to merge those two tables into a single table. To do that you can select all rows from the one table and insert them into the other, then delete the unneeded table. If you need to distinguish between the two types of qualification you can add a column for that. Here's some example SQL for copying data from one table to the other:
INSERT INTO qualifications (col1, col2, ... coln, is_additional)
SELECT col1, col2, ... coln, 1
FROM addtional_qualifications
Merging two tables can be tricky if you have an AUTO_INCREMENT PRIMARY KEY column with the same values in both tables, and other tables refer to these values via foreign key constraints.

Related

Problems generating random data in SQL workbench

I am new using mySQL, so probably my question will be very banal, but I didn't find any solution on internet.
I have two tables, TABLE 1 and TABLE 2, each one with a single primary key tab1PK (INT) and tab2PK (VARCHAR).
Since TABLE 1 and TABLE 2 have a M:N relationship, I have a third table, TABLE 3, whose PK are two: tab1PK and tab2PK.
I generated random data for TABLE 1 and TABLE 1. Is there a way to generate rapidly data for the TABLE 3? Is there a way to easily combine tab1PK and tab2PK?
This will give you a cartesian join of all table1 & table2 primary keys.
insert into table3 (tab1PK, tab2PK)
select table1.tab1PK, table2.tab2PK
from table1, table2

MySQL merge multiple tables that share similar columns

I have 5 tables, each with 10+ columns. They all share some similar columns but each table has columns unique to themselves. The tables are not related to each other, meaning that one record in one table won't be able to exist in another table. Therefore, when I try to concatenate them using UNION ALL, I get a very complicated MySQL statement since the tables have so many columns. For example, I have 2 tables:
Table1: ID, Name, Parts, Comments, End_Date
Table2: ID, Name, Machines, End_Date
If I were to combine these two tables, I would use the MySQL statement
SELECT
ID,
Name,
Parts,
'' as Machines,
Comments,
End_Date
FROM Table1
UNION ALL
SELECT
ID,
Name,
'' as Parts,
Machines,
'' as Comments,
End_Date
FROM Table2
As you can see, the statement gets much larger the more columns and more tables there are. Is there an elegant way to concatenate these tables in a more concise statement? Thanks!
For combining tables, you can use the FOREIGN KEY method from SQL that will work in MySQL. To do this, outside of the creation of your table:
ALTER TABLE Table1
ADD FOREIGN KEY (End_Date) REFERENCES Table2(End_Date);
To do this inside the creation of your table, you would just code it in after your primary key and use the bit that includes FOREIGN KEY and everything after except the ' ; '. This method will only work with one column from each table though.
For extra help that you could research if you so choose to, this website has the basic methods of all SQL languages and is extremely helpful:
SQL Tutorials

How to merge two db with same structure but different data

I'm working with phpmyadmin and I have to merge two db with same structure but different data.
The db have relation between tables (foreign key).
The data in two db may have same id, and so their foreign key.
I would like to know if it's possible merge the two db keeping all data, so, if a row already "exist", insert it with new id and update its foreign key.
thanks a lot
No easy way unfortunately. If you have TableA as a foreign key to TableB, you will need to
1) Insert data from source tableA to target tableA
2) create a (temp) table to store the mapping between source tableA ids and target tableA ids
3) Use this mapping table when inserting data from tableB to convert the tableA ids to the new ones in the target db
... and so on. It can get quite hairy if you have a deep hierarchy of tables, but hopefully you get the idea. Take backups before you start.
Another idea that you might want to consider is using a cursor:
Assume table A is the one that you want to keep and table B is the one you want to remove.
Declare a cursor for table B and select all the records.
Loop each record selected from the cursor and check.
Case 1: If the ID is exists on table A, insert the record to table A with same details.
Case 2: If the ID is exists on table B, insert the record and modify the ID and foreign key.
Once all the records have been checked, drop table B.
Sorry, I just can give an idea at the moment.

Inserting into a table from an incompatible table

I have a MySql table called Person, and one day I accidentally deleted someone from this table. I have a backup table, called PersonBak so I was going to restore my deletion from the backup. However, in the course of moving forward on my application I renamed all the fields in Person, except for the primary key, PersonID. Now Person and PersonBak have the same data, but only one matching column name.
Is there any way to restore my missing person to Person from PersonBak without doing a lot of work? I have quite a few columns. Of course I could just do the work now, but I can imagine this coming up again.
Is there some way to tell MySql that these are really the same table, with the columns in the same order, just different column names? Or any way at all to do this without writing out specifics of which columns in PersonBak match which ones in Person?
If the column datatypes are the same between the tables, the column count is the same, and they are all in the same order, then MySQL will do all of the work for you:
INSERT INTO t1 SELECT * FROM t2;
The column names are ignored. The server uses ordinal position only, to decide how to line up the from/to columns.
What about this:
insert into Person(id, col11, col12) (select id, col21, col22 from personBak where id=5)
person schema:
columns (id, col11, col12)
personBak schema:
columns (id, col21, col22)
Look at Mysql SELECT INTO and you can specify the field names & create an insert statement

MSQL creating a table which automatically updates with data from other tables

I have two tables:
Table1 - Sales
id, Timestamp, Notes, CSR, Date, ExistingCustomer, PubCode, Price, Pub, Expiry, Size, KeyCode, UpSell, CustName, CustAddress, CustCity, CustState, CustPostal, CustCountry, CustPhone, CustEmail, CustCardName, CustCardNumber, CustCardType, CustCardExpiry, CustCardCode
Table2 - Refunds
id,Timestamp,CSR,Date,OrderId,Refunded,Saved,Publication
Basically, I want to create a table (MySQL) which will have some columns that are the same between the two tables and which will update automatically with the values from these two columns.
ie. Table3
Timestamp, CSR, Date, Publication
And this table would automatically update whenever a new record is posted into either of the other two tables, so it would essentially be a merged table.
Because there's nothing to join these two tables, I don't think the JOIN function would work here. Is there anyway I can do this?
You can use an trigger which actives on insert on both tables to make it automatically update.
As for combining tables with no common tables, view this question.
You need to use a stored procedure and a trigger on insert/update of the non merged table
There's got to be some way to join it, and in fact you mention Timestamp, CSR, Date, Publication.
You could join on them in a view. You could add table three and then add triggers though that would be an awful mess.
Why do you want to denormalise in this way?
How about Table3 is a unique key to use as a surrogate, and your 4 join fields, and then you take those out of Table 1 and 2 and replace them with the key the suurrogate key in table 3.
Then it'sa simple join query and no data duplication.