Transfer records between database with overlap ID (Primary Key) - mysql

My current project need to transfer data between databases. The scenario is that I have two different database, they have the similar schema for tables but the information inside table is completely different.
I want to transfer one record from one database to the other, and completely removed it in the old database once transferred. I realize that the primary key for that record may overlap with the one inside the new database.
I think of many solutions: I can add a character in front of the id to distinguish data from different table, then how can I add the character (for example B1100) in front of the id when I transfer? Or how do I automatically increase the ID (meaning generating new ID with the correct order in the new database).
For example: TABLE1 and TABLE2. TABLE1 has the most recent ID is 2000, and I want to transfer a record from TABLE2 with the ID is also 2000. Is there a way that I can transfer that record from table2 with table2.ID is 2000 to a new record of TABLE1 with table1.ID is 2001?
Thank you!
I am planning to use this SQL Query for transferring:
INSERT INTO DATABASE_1.dbo.table1
SELECT *
FROM DATABASE_2.dbo.table2
WHERE DATABASE_2.dbo.table2.id = currentID;

Related

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.

Concurrent Inserts in mySQL

I have 3 table - TB1, TB2 and r_tb1_tb2 (innoDB)
TB1 Hold the details of the users (will be inserted)
- id (primary, unique)
- name
TB2 Holds the details of the course the users can take (static table)
- id (primary, unique)
- name of the course
r_tb1_tb2 hold the relation between the 2 tables
- rID
- user_id (from table 1)
- course_id (reference to table 2)
When I insert a new row in TB1, I get the id of the last inserted row.
And use that to insert another row in r_tb1_tb2
I can forsee that this may result to erroneous entries in case of simultaneous instances of inserts in tb1.
Can someone please point to the best practices for such simultaneous updates.
Thanks in advance.
last_insert_id has built in protection for this
The ID that was generated is maintained in the server on a
per-connection basis. This means that the value returned by the
function to a given client is the first AUTO_INCREMENT value generated
for most recent statement affecting an AUTO_INCREMENT column by that
client. This value cannot be affected by other clients, even if they
generate AUTO_INCREMENT values of their own. This behavior ensures
that each client can retrieve its own ID without concern for the
activity of other clients, and without the need for locks or
transactions.
(emphasis theirs)
Thus if two different users are taking action on your site that results in records being inserted into T1, the last_insert_ids for those users will be different because they are using two different connections (clients in the conext above)
last_insert_id return value according the the user:

Importing sql file data omitting columns (MySql)

I am building a new booking system in PHP at the moment and I want to take over most of the data of the old system (MySQL) however the database structure of the new system will be slightly different. However I think I could take over some of the tables. Is it possible to import rows of old tables to the new table given that the column names are equal but some column names in the new table might be missing?
an example, the old table:
tourguides
id name address email telephone_mobile telephone_home
the new table
tourguides
id name address telephone_mobile
the new table doesn't has the column telephone_home, this shouldnt stop the import but instead it should be ignored
Yes, it's possible.
You should do an INSERT SELECT with fixed fields, like this:
INSERT INTO table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM table2
You can even do it between databases:
INSERT INTO db1.table1 (fieldX, fieldY) SELECT fieldX, fieldY FROM db2.table2

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

VBA code for deleting and creating relationships adding same ID records in the child table, is it possible?

I have a table that I create every time with updated data from a main database.
so there is a MAIN database with all the data, from that I query and get a dataset which I save as a table lets call it (TABLE1), in a small access database.
The MAIN database gets updated manually by me and some other colleagues and then I delete the old table on my access database and create a new one (TABLE1), with the updated data.
The updated data represents new records, and some updated fields on existing records.
I have another table (TABLE2), on my access database with some extra data related to the records in (TABLE1).
Every time I do this I have to break up the relationship link I have within the tables ID fields via VBA.
Then I have to add empty rows in TABLE2 to match TABLE1 and recreate the relationship.
Can it be done better with VBA code?
Example:
TABLE1
-------
name
-------
'cat'
'dog'
'mouse'
TABLE2
------
cost
------
23
13
25
Query based oon ID fields (autonumber)
-------|-----
name |cost
-------|------
'cat' |23
'dog' |13
'mouse'|25
You should not need to delete TABLE1 so that you can then recreate it with a fresh set of data. Instead, discard the existing rows ...
DELETE FROM TABLE1;
... then append your new rows to it ...
INSERT INTO TABLE1 (
fld1,
fld2,
etc
)
SELECT
fld1,
fld2,
etc
FROM MAIN
WHERE <the condition which identifies the new records>;
However I'm unsure how that applies to the existing relationship. If you're not enforcing relational integrity, that may be enough.
If you still need to delete the relationship, you can remove it from your database's Relations collection.
Dim db As DAO.Database
Set db = CurrentDb
db.Relations.Delete "YourRelationName"
Set db = Nothing
Later, to recreate the relationship you can use the Database.CreateRelation Method. That will likely be more involved than deleting the relationship. See this detailed example: VBA code for creating MS Access Relations