How to merge two mySQL database into one? - mysql

I have two database, each contain different tables
leave.sql and crm.sql
I want to transfer all my leave tables into the crm database
How should I do it?

If the other database has different tables then the leaves database then all you need to do is import the data into the crm database. Finally, make sure your leaves script is connected to the new crm database.

Two other methods:
On the mysql prompt, you can rename your tables - and effectively move them from one database to the other (if both are on the same filesystem):
From http://dev.mysql.com/doc/refman/5.0/en/rename-table.html:
As long as two databases are on the same file system, you can use
RENAME TABLE to move a table from one database to another:
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
If you are using MyISAM tables, and you have root privileges, you can just do:
stop your mysql server
move your tables from the old database directory to the new one
start your mysql server
All those assuming the tables in your old database (the ones you want to move) have different names than the ones in your new database (the ones you want to keep). If that's not true, you'll also have to change the names.

Related

Accesing data from one mysql database to another in MYSQL Workbench

I have two different databases. I have to access data from one database and insert them into another ( with some data processing included, it is not only to copy data ) Also, the schema is really complex and each table has many rows, so copying data into schema in the second database is not an option. I have to do that using MySQL Workbench, so I have to do it using SQL queries. Is there a way to create a connection from one database to another and access its data?
While MySQL Workbench can be used to transfer data between servers (e.g. as part of a migration process) it is not useful when you have to process the data first. Instead you have 2 other options:
Use a dedicated tool you write yourself to do that (as eddwinpaz mentioned).
Use the capabilities of your server. That is, copy the data to the target server, into a temporary table (using dump and restore). Then use queries to modify the data as you need it. Finally copy it to the target table.

Creating an independent copy of a MySQL database

When you dump the database 'A' using mysqldump and imported into 'A_test', would they be two independent copies of the same database? Or changes to one of them would also affect the other one?
No, they become two different identical database, and changes on one does not reflects on the other, after the dump and import procedure.
If you create any data in database B, then it is completely independent of database A. As far as I know, there is no way to connect tables of different databases in any way.
if you want to make sure nothing bad happens,
create a user B that only has access rights to database B.
Use user B to import the database into B.
Use user B to access database B from your app.
User B cannot change anything belonging to database A.
(you always should create user/database pairs with the same name)
You have two independent databases. Database 'A' AND you also have database 'A_test'. And quires made to one will not affect the other unless you reference both in the query itself. I.E. if you add an entry to 'A', it will not also be added to 'A_Test' unless you also add it to 'A_Test'.

export tables of mysql database for insertion into a different database

I have a wordpress instance running on my localhost. In order to move this to another server, I'd like to export the contents of this database to an sql file. However, I don't have the permission to create new databases, I have an existing database in which I'd like to insert the tables and all the rows inside them. Is there a way to tell phpmyadmin to export the data in such a way that everything will be inserted into this new database? Or would it be better to just do a find/replace inside the sql file?
In fact, if you select the database in phpmyadmin, it shows all the tables contained. If you choose to export at that stage, it will export by default (actually depends on its version) all the tables structure and data without database creation. Additionally, it does give you the option of exporting only the data.

How to merge different MySQL databases?

I have an old database which I want to import in the new one but they are different in columns. How do I import it without any loss or change to the old database ?
First of all, figure out which tables (and their columns) in the old DB match which tables (and their columns) in the new DB.
Then, create a sequence of INSERTs into the new DB with SELECTs from the old DB...
Check out this part of MySQL documentation dealing with INSERT...SELECTs.

Combine several mssql database to one mysql with php

We are handling a data aggregation project by having several microsoft sql server databases combining to one mysql database. all mssql database have the same schema.
The requirements are :
each mssql database can be imported to mysql independently
before being able to import each record to mysql we need to validates each records with a specific createrias via php.
each imported mssql database can be rollbacked. It means even it already imported to mysql, all the mssql database can be removed from the mysql.
we would still like to know where does each record imported to the mysql come from what mssql database.
All import process will be done with PHP .
we have difficulty in many aspects. we don't know what is the best approach to solve our problem.
your help will be highly appreciated.
ps: each mssql database has around 60 tables and each table can have a few hundred thousands .
Don't use PHP as a database administration utility. Any time you build a quick PHP script to transfer records directly from one database to another, you're going to cause yourself a world of hurt when that script becomes required for production operation.
You have a number of problems that you need solved:
You have multiple MSSQL databases with similar if not identical tables.
You have a single MySQL database that you want to merge the data into.
The imported data must be altered in a specific way before being merged.
You want to prevent all duplicate records in your import.
You want to know what database each record originally came from.
The solution?
Analyze the source MSSQL databases and create a merge strategy for them.
Create a database structure on the MySQL database that fits the merge strategy in #1, including all the new key constraints (like unique and foreign keys) required for the consolidation.
At this point you have two options left:
Dump the data from each of the source databases into raw data using your RDBMS administration utility of choice. Alter that data to fit your merge strategy and constraints. Document this, and then merge all of the data into your new database structure.
Use a tool like opendbcopy to map columns from one database to another and run a mass import.
Hope this helps.