How to insert data from one database table to another database table? - mysql

I have to MySQL databases DB1 and DB2, DB1 is on Online server, and DB2 is on local machine(localhost), Now i want to insert some data into DB2's table named db2_table from DB1's table named db1_table using SQL QUERY. So how it is possible?

I think this is not possible with one SQL Query. A SQL-Query can only be executed on one server and he doesn't know the second server. So you have to do this with an application or you have to use the import and export functions of MySql Workbench or PHPMyAdmin.
Here you can find a very similar question:
insert into a MySQL database on a different server
Hope this will help you.

It may be possible to create 2 simultaneous connections as suggested by #Being Human - and write one query - which would be ideal. However the method below works for me.
1: make a dump file of DB1 from online server
mysqldump -u <db_username> -h <db_host> -p db_name table_name > backup.sql
2: restore dumpfile to local server and database (will restore to same DB)
mysql -u<db_username> -p DB_NAME < backup.sql
3: INSERT into db2_table from db1_table
INSERT INTO DB2.db2_table (<col1>, <col2>, <col3>....)
SELECT <col1>, <col2>, <col3> ...
FROM db1_table;
Making sure the columns returned by the SELECT subquery on db1_table maps into the columns in the db2_table

Related

How to clone MySQL database under a different name with the same name and the same tables and rows/content using SQL query

I know how to clone tables e.g.:
CREATE TABLE recipes_new LIKE production.recipes; 
INSERT recipes_new
SELECT * FROM production.recipes;
But I don't know how to clone e.g. a database_old to database_new database with all the tables and rows from database_old.
So, only the name of the database will change. Everything else stays the same.
Right now I am cloning it by exporting the database in phpmyadmin ad then creating a new database and importing it to the new database.
But I guess there must be a more efficient way of doing this task via SQL query like that one for cloning tables.
IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.
Thanks in advance for you suggestion how to do that.
have you tried using MySQL Dump?
$ mysqldump yourFirstDatabase -u user -ppassword > yourDatabase.sql
$ echo "create database yourSecondDatabase" | mysql -u user -ppassword
$ mysql yourSecondDatabase -u user -ppassword < yourDatabase.sql
IMPORTANT! It need to be done from SQL query window in phpmyadmin and not from a shell command line.
First create a blank database:
CREATE DATABASE `destination` DEFAULT CHARACTER SET
latin1 COLLATE latin1_swedish_ci;
Then use the command show tables;
show source.tables;
and then run the command for each DB table (Optimized Create table and inserting rows) as:
create table destination.table select * from source.table;
and other way is using like command:
create table destination.table like source.table
and then inserting rows;
insert into destination.table select * from source.table
If phpMyAdmin is available for the database, you can clone it by following these steps:
Select required database
Click on the operation tab
In the operation tab, go to the "copy database"-option and type your desired clone-db-name into the input field
Select "Structure and data" (Depends on your requirement)
Check the boxes, "CREATE DATABASE before copying" and "Add AUTO_INCREMENT value"
Click "GO"
Tested with phpMyAdmin 4.2.13.3
export your chosen database using phpmyadmin (export.sql)
import it using terminal/cmd:
mysql -u username -p databasename < export.sql
and get a cup of coffee...

Restoring selective tables from an entire database dump?

I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes)
How would I restore those two tables without restoring the entire database?
Well, you have three main options.
You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.
Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).
Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.
Which one you choose will depend upon the exact situation (including how much data you have)...
You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.
Open the .sql file and copy the insert statements for the tables you want.
create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.
What you want is a "Single Table Restore"
http://hashmysql.org/wiki/Single_table_restore
A few options are outlined above ... However the one which worked for me was:
Create a new DB
$ mysql -u root -p CREATE DATABASE temp_db
Insert the .sql file ( the one with the desired table ) into the new DB
$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql
dump the desired table
$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql
import desired table
$ mysql -u root -p original_database < ~/awesome_single_table.sql
Then delete the temp_db and you're all golden!

Copy data from 1 DB to another DB

I have to delete some table data in prod. db and for the records that are going to be deleted a backup of records should be copied to another local db. This involves two databases, residing in two different servers/instances.
Is it possible to do via sql (mysql) query to do this?
I would use mysqldump with a where condition to get the records out. Once you have everything saved, you can then delete them from prod. These commands should work from the command line, including the password to avoid the prompt is optional.
mysqldump -u user -pPassword -h hostname1 dbname tablename
--where 'field1="delete"'
--skip-add-drop-table --no-create-db --no-create-info > deleted.sql
mysql -u user -pPassword -h hostname2 dbname < deleted.sql
mysql -u user -pPassword -h hostname1 dbname -e 'DELETE FROM tablename WHERE field1="delete"'
I'm trying to do exactly the same thing, copy data from a table to another server, then delete it from the original.
So far I see two options:
copy data to a local database then replicate that database to another server
use Federated storage engine
Both require some serious reconfiguration of our servers as neither Federated or binary logging (required for replication) are not enabled. This would take time and it would be best if other solutions could be found.
The process needs to be executed on a daily basis, so it needs to be fully automated.
Perhaps a third option is to automate things with a cron job:
copy the data to a separate database on the same server
backup that database with mysqldump in a folder which is linked on the other server too
on the second server, restore the database from the sql dump

Generating SQL script from MySQL database table

Is there any application that will read a MySQL database table and generate a SQL script of INSERT statements (so that I can copy tables from one db to another db)? OR how can I transfer content from db1.table1 to db2.table2 where table1 and table2 is same.
Thank you.
mysqldump [options] db_name [tbl_name ...]
Will generate the script file including the create and inserts necessary for the tables selected. To import the dump you can simply do:
mysql -u <user> -p dbname < mys.dmp
You should take a look at mysqldump. You can specify the --nodata option to export the schema only.

Mysql restore to restore structure and no data from a given backup (schema.sql)

Hi I use mysql administrator and have restored backup files (backup.sql). I would like to use restore the structure without data and it is not giving me an option to do so. I understand phpadmin provides this. I can not use this however. Any one can tell me an easy way?
Dump database structure only:
cat backup.sql | grep -v ^INSERT | mysql -u $USER -p
This will execute everything in the backup.sql file except the INSERT lines that would have populated the tables. After running this you should have your full table structure along with any stored procedures / views / etc. that were in the original databse, but your tables will all be empty.
You can change the ENGINE to BLACKHOLE in the dump using sed
cat backup.sql | sed 's/ENGINE=(MYISAM|INNODB)/ENGINE=BLACKHOLE/g' > backup2.sql
This engine will just "swallow" the INSERT statements and the tables will remain empty. Of course you must change the ENGINE again using:
ALTER TABLE `mytable` ENGINE=MYISAM;
IIRC the backup.sql files (if created by mysqldump) are just SQL commands in a text file. Just copy-paste all the "create ..." statements from the beginning of the file, but not the "insert" statements in to another file and "mysql < newfile" you should have the empty database without any data in it.
there is no way to tell the mysql client to skip the INSERT commands. the least-hassle way to do this is run the script as-is and let it load the data, then just TRUNCATE all of the tables.
you can write a script to do the following:
1 : import the dump into a new database.
2 : truncate all the tables with a loop.
3 : export the db again.
4 : now u just have the structure
You can backup you MYSQL database structure with
mysqldump -u username –p -d database_name > backup.sql
(You should not supply password at command line as it leads to security risks.MYSQL will ask for password by default.)