I have a mysql database with InnoDB tables that I need to copy into a newly created database. I would like to automate the copying process with a script since there are over 100 tables.
This is my current code. After 10 tables, it's getting tiresome:
> use newdb;
> CREATE TABLE tableA LIKE old_db.tableA
> INSERT INTO tableA SELECT * FROM old_db.tableA
How can I extract the table names from old_db in order to automate this process?
Use MySQLWorkbench to export the schema and data from the source database and then import into the new database.
Related
I have 2 MYSQL server A and B, and both have a table of the same name
and same DB schema.
Server A has 3000 rows while server B has 10000+rows.
In regarding these rows, they do not have any overlap. Both are unique dataset.
And I have already execute "mysqldump ... " to have Server A's data dumped in a file, so what is the easy way for me to load Server A's data on this table into Server B, on top of the existing Server B dataset ? Is there a command way to do that ?
When making the dump, use the --no-create-info option. This leaves out the DROP TABLE and CREATE TABLE statements in the dump file that would remove the existing table.
The dump file will then just contain the INSERT statements, which will append to the existing table.
If you can't recreate the dump file, open it in an editor and delete those statements.
I have a view in my production replica (it's a read-only database) and I need to copy one of the views to my data warehouse.
What would be the best way to go about it?
I can not create a table from the view on the replica. Meaning I can not do CREATE TABLE Table_From_View AS SELECT * FROM My_View;
I can not copy all the tables that create the view definition. Some of them contain sensitive data.
I am sure I missing something basic here...
Ideas?
In MySQL, a view doesn't store anything. It's more like an alias or a macro. It just passes the query logic through to underlying base tables.
You need the underlying base tables to exist on the same MySQL instance where you create the view, and the base tables must remain existing on that same MySQL instance while you query the view.
If you only want to copy the contents of a view, so you don't copy the other sensitive data that is not selected by the view, you would have to create a base table to copy the data:
mysql> CREATE TABLE myview_base AS SELECT * FROM myview;
Then you can do a logical dump of that copy table:
shell > mysqldump --single-transaction mydatabase myview_base > myview_base.sql
Then restore that dump file to your data warehouse as you would any other SQL dump file.
Another possible strategy:
SELECT * FROM myview INTO OUTFILE 'filename.csv';
This dumps the result of an SQL query to a file. See https://dev.mysql.com/doc/refman/8.0/en/select-into.html
The file will be created on the database server, so if you don't have shell access to the server, you won't be able to retrieve the file.
I just want to rename some tables in the same mysql database using bash script, so I can call it through the cronjob.
The table is in innodb storage, I can do the same in phpmyadmin in < 1 sec. for rename any table. Would like to do the same using bash script. it is way faster than copy to a new table with new name.
Just need to get the right synatax for it. I know the db and table name.
Thanks,
echo "rename table t1 to t2" | mysql ... db
I wrote a PHP script to list all the tables present in the database using a query
$query = "SHOW TABLES FROM $dbName";
Now, i wanted to know what sql query can be use to list the "contents" of all the tables from the database simultaneously to display it and then store it in a file.
Thanks ahead of time..
You can use mysqldump to dump all the data in the database. Else you can run for look on all the table names and then use select * from x; to get all the data from the table name x which you retrieved earlier.
IF what you want is to backup your database and restore it check out this link
MYSQL backup and restore
and you can use this command to create a backup of your databse
MYSQLDUMP
I want to copy the data from one MySQL table to another table. The source table contains 30 million records. the SQL connection gets lost when I tried to copy the data using the SQL query
INSERT table2 SELECT * FROM table1
Is there any external tool avaliable to do this job from the shell
Thanks
Sree
The mysql command line tool should be able to handle this just fine.