Generating SQL script from MySQL database table - mysql

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.

Related

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

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

mysqlimport using dump

I need to restore a dumped database, but without discarding existing rows in tables.
To dump I use:
mysqldump -u root --password --databases mydatabase > C:\mydatabase.sql
To restore I do not use the mysql command, since it will discard all existing rows, but instead mysqlimport should do the trick, obviously. But how? Running:
mysqlimport -u root -p mydatabase c:\mydatabase.sql
says "table mydatabase.mydatabase does not exist". Why does it look for tables? How to restore dump with entire database without discarding existing rows in existing tables? I could dump single tables if mysqlimport wants it.
What to do?
If you are concerned with stomping over existing rows, you need to mysqldump it as follows:
MYSQLDUMP_OPTIONS="--no-create-info --skip-extended-insert"
mysqldump -uroot --ppassword ${MYSQLDUMP_OPTIONS} --databases mydatabase > C:\mydatabase.sql
This will do the following:
remove CREATE TABLE statements and use only INSERTs.
It will INSERT exactly one row at a time. This helps mitigate rows with duplicate keys
With the mysqldump performed in this manner, now you can import like this
mysql -uroot -p --force -Dtargetdb < c:\mydatabase.sql
Give it a Try !!!
WARNING : Dumping with --skip-extended-insert will make the mysqldump really big, but at least you can control each duplicate done one by one. This will also increase the length of time the reload of the mysqldump is done.
I would edit the mydatabase.sql file in a text editor, dropping the lines that reference dropping tables or deleting rows, then manually import the file normally using the mysql command as normal.
mysql -u username -p databasename < mydatabase.sql
The mysqlimport command is designed for dumps created with the mysql command SELECT INTO OUTFILE rather than direct database dumps.
This sounds like it is much more complicated than you are describing.
If you do a backup the way you describe, it has all the records in your database. Then you say that you do not want to delete existing rows from your database and load from the backup? Why? The reason why the backup file (the output from mysqldump) has the drop and create table commands is to ensure that you don't wind up with two copies of your data.
The right answer is to load the mysqldump output file using the mysql client. If you don't want to do that, you'll have to explain why to get a better answer.

Copying an existing table data from read only database into read-write database MySQL

I have two databases, for one I have only read access, and for the other i have read-write access. Let's call them A and B respectively.
There is a table in A that I need to copy(with its data and structure) into B.
To copy its structure I have used 'LIKE' keyword, but it did not work out. So, i could not copy the data also.
Do you guys have any further suggestions?
Thank you in advance.
To copy a table from one database to another, you'd do:
DROP TABLE IF EXISTS backup_db.table1;
CREATE TABLE backup_db.table1 SELECT * FROM live_db.table1;
Or you could use mysqldump
mysqldump -u root -p --opt dbname table1 > ~/export.sql
And to put it into the other database, you'd do:
mysql -u root -p dbname2 < ~/export.sql
Edit: Also, please note, if you had a master and slave setup, this would happen automatically. A slave is read-only while a master is read/write. Data from the master would automatically be copied to the slave, but you'd need two servers. Just for your information.
Take a look at mysqldump. It will allow you to backup the table data/structure to file from server A and restore it on server B.
mysqldump [options] db_name [tbl_name ...]

Is it possible to extract a script from an MySQL database to create a copy of the tables on another MySQL database?

Is it possible to extract a script from an MySQL database to create a copy of the tables on another MySQL database which is located on a remote server?
You can dump either the structure of the tables, or their content, using the mysqldump command-line tool.
For example, to get a dump of the structure (create table instructions) of all tables of a database, you can use something like this :
mysqldump --no-data --user=USER_NAME --password=PASSWORD --host=HOST DATABASE_NAME
And, to get this to a file, instead of getting it to the standard output, you can redirect that output :
mysqldump --no-data --user=USER_NAME --password=PASSWORD --host=HOST DATABASE_NAME > dump-file.sql
If you also want the data of the tables, and not just their structure, do not use the --no-data option.
Then, on your other server, you can import the dump, using :
mysql --user=NEW_USER_NAME --password=NEW_PASSWORD --host=NEW_HOST NEW_DATABASE_NAME < dump-file.sql
Do you mean to extract a 'dump'?
yes of course. mysqldump utility is the general choice

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