In MySQL, how do I batch rename tables within a database?

余生颓废 提交于 2020-05-08 03:00:08

问题


So basically I have a joomla database in MySQL which has a bunch of tables that have the prefix 'jmla_'. I would like to rename all of these tables by replacing the 'jmla_' prefix with a 'jos_' prefix. Any ideas about how to do this with a simple SQL script or SQL query?


回答1:


SELECT  concat ('rename table ',table_name,' to ',table_name,'_old;')
FROM information_schema.tables
WHERE table_name like 'webform%'
  and table_schema='weiss_db_new'

will work.




回答2:


Run this statement:

SELECT 'rename table '||table_name||' to '||'jos'||substr(table_name,5)||';'
FROM information_schema.tables
WHERE table_name like 'jmla%'

This creates a script that will rename all the tables. Just copy & paste the output into your SQL client.

(You will need to change the || to MySQL's non-standard concatenation operator in case you are not running it in ANSI mode)




回答3:


RENAME TABLE jmla_whatever to jos_whatever;

You'll have to write a script to cover all your tables - you can populate your script with the output of show tables. See http://dev.mysql.com/doc/refman/5.0/en/rename-table.html for details.




回答4:


  1. Export using phpmyadmin to .sql file
  2. Use any text editor (I prefer vim, work excellent on large files) with function "find and replace" to open file
  3. Do "find and replace", putting your actual prefix in find box, and updated in replace box
  4. Import file using phpmyadmin.

Remember to drop old database before importing by phpmyadmin. This may be done, checking suitable options during export.




回答5:


Have you considered using Akeeba Admin Tools Component for Joomla! and then using the Database Prefix Editor to change table prefixes, works really well. Akeeba Admin tools can be used to secure your Joomla! installation

Akeeba can be obtained here



来源:https://stackoverflow.com/questions/10066783/in-mysql-how-do-i-batch-rename-tables-within-a-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!