Rename a table in mysql using bash script - mysql

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

Related

Does MYSQLDump create temp tables?

I'm on MySQL 5.7
I have a table that is about 150GB, the storage on the computer is only 200GB.
So I wanted to get rid of data older than 9 months on this table.
So my plan was to take a dump of the table with the where clause. Then truncate the table, and reinsert the dump.
Does creating a dump with a where clause create a temp table, where I would run out of storage before being able to export all that data?
What I ran into where I tried regular delete statement was table locking and storage filling up quickly from temp table being created to delete. At least I think this is what happened when I tried to just delete
You can make mysqldump run without using any temp space. Use the --opt switch on the command line. At a minimum use the --quick switch.
You can use a simple WHERE clause and it will still work.
And be sure to run the command on a machine with enough hard drive space to store the output .sql file.

how to load dataset on top of existing dataset in MySQL DB?

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.

Need insight into script to copy InnoDB tables to another database

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.

SQL Query to list the contents of all the tables from the database

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

Copying MySQL table data to another table

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.