Difference between revisions of "Renaming MySQL tables and databases"
(renaming tables) |
m (→Renaming tables) |
||
Line 19: | Line 19: | ||
But usually we need to rename forty or fifty tables because we're usually changing the table-prefix of all the tables in an application like MediaWiki or Wordpress. To do this we can output the list of tables we want to change with SHOW TABLES, and then use a text editor (one that has regular expressions like [[Geany]]) to reformat the list into a RENAME TABLE command that we'll paste back into the MySQL command line. First log in to MySQL, select the database containing the tables and list them as follows (in this example we're going to change a prefix from ''foo_'' to ''bar_''. | But usually we need to rename forty or fifty tables because we're usually changing the table-prefix of all the tables in an application like MediaWiki or Wordpress. To do this we can output the list of tables we want to change with SHOW TABLES, and then use a text editor (one that has regular expressions like [[Geany]]) to reformat the list into a RENAME TABLE command that we'll paste back into the MySQL command line. First log in to MySQL, select the database containing the tables and list them as follows (in this example we're going to change a prefix from ''foo_'' to ''bar_''. | ||
− | {{code|< | + | {{code|<mysql>USE DB_A |
− | SHOW TABLES LIKE foo_%;</ | + | SHOW TABLES LIKE foo_%;</mysql>}} |
Revision as of 22:17, 4 October 2012
Renaming MySQL tables and databases Organic Design procedure |
Duplicating a database
This example duplicates a database called "DB_A" to "DB_B", to rename you'd also drop the original database after successfully duplicating it.
First log in to MySQL and create a new empty database for the target name
Then quit from MySQL and export the source database to a temporary file, and import it into the new database. I prefer to do this as two separate commands so that we can use interactive passwords and avoid them being written to history.
Renaming tables
Renaming one or two tables is simple, just log in to SQL and use the following syntax:
But usually we need to rename forty or fifty tables because we're usually changing the table-prefix of all the tables in an application like MediaWiki or Wordpress. To do this we can output the list of tables we want to change with SHOW TABLES, and then use a text editor (one that has regular expressions like Geany) to reformat the list into a RENAME TABLE command that we'll paste back into the MySQL command line. First log in to MySQL, select the database containing the tables and list them as follows (in this example we're going to change a prefix from foo_ to bar_.
Then select and copy the table list and paste into your text editor, you should have something like the following (I've omitted most tables to save space).
Then you can use the following regular expression to convert the list into the appropriate format.
The search expression is:
and the replacement expression is:
Now you should have the following text in the editor: