SQL RENAME TABLE Command

I can run the RENAME TABLE student TO student_new ;
command is the same and easy to follow.

Are there methods to rename a large number of tables in a simple command. Suppose all tables belong to the same DB name.

I do not need to write a lot of code, as shown below:

RENAME TABLE pre_access TO pre_new_access; 
RENAME TABLE pre_activities TO pre_new_activities;
RENAME TABLE pre_activityapplies TO pre_new_activityapplies;
RENAME TABLE pre_adminactions TO pre_new_adminactions;
RENAME TABLE pre_admincustom TO pre_new_admincustom;
RENAME TABLE pre_admingroups TO pre_new_admingroups;
RENAME TABLE pre_adminnotes TO pre_new_adminnotes;
...

(there are still so many tables to rename)

Update: MySQL is in use.

+2
source share
4 answers

Assuming that from your comment that you are using MySQL, you should be able to "select" the necessary statements from information_schema.tables, which contains a list of your tables.

your_schema .

Select Concat( 'RENAME TABLE ', table_name, ' TO ', 'new_', table_name, ';' )
From information_schema.tables
Where table_schema = 'your_schema';

RENAME TABLE c_data TO new_c_data;
RENAME TABLE c_main TO new_c_main;
...
+5

PHP script, REGEX.

+2

, , ( ). , - , RENAME, , , , .

+1

mysql "test" :

echo "show tables"|mysql test|perl -ne 'if(/pre_(\w+)/){print "rename table pre_$1 to pre_new_$1;\n"}'|mysql test
0

All Articles