My provider installed to my site Drupal CMS. Now I need copy all my data from old site. I have tables without prefixes in my old DB, but in new DB all tables have dp_[table_name]
prefix.
7 Answers
zerkms solution didn't work for me. I had to specify the information_schema
database to be able to query the Tables
table.
SELECT
CONCAT('RENAME TABLE ', GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, '` TO `', TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`')) AS q
FROM
`information_schema`.`Tables` WHERE TABLE_SCHEMA='test';
Edit:
Optimized the query to only call RENAME TABLE once. Something I walked into was the fact that the concatenated output got truncated at 341 characters. This can be solved (if allowed by your server) by setting the MySQL variable group_concat_max_len
to a higher value:
SET group_concat_max_len = 3072; -- UTF8 assumes each character will take 3 bytes, so 3072/3 = 1024 characters.
-
True, this worked for me since I was working on localhost XAMPP using Joomla and I wanted to change to auto Joomla prefix. I follow your steps- I had to specify the
information_schema
.Tables
as you did and set SET group_concat_max_len = 10240; in my case since there were many tables. +1 Feb 1, 2015 at 8:59 -
1One more point, you can use same command to move table across databases (say backup_db) like:
SELECT CONCAT('RENAME TABLE ',TABLE_NAME,' TO backup_db.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'testbot';
– sactiwJun 10, 2016 at 11:32 -
great point the edit about the
group_concat_max_len
variable! I wasn't getting the full sentence neither, worked great!– AlexGHFeb 26, 2018 at 16:38
PhpMyAdmin allows you to do this now. At the "Database" level select the Structure tab to see all the tables. Click 'check all' (below the table listing). On the 'With selected' dropdown choose: 'Replace table prefix'.
write a script that will run RENAME TABLE for each table.
SELECT
GROUP_CONCAT('RENAME TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` TO `', TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`;' SEPARATOR ' ')
FROM
`TABLES` WHERE `TABLE_SCHEMA` = "test";
where "test" is expected database name
after this you can long query that will add prefixes if you execute it ;-)
You can simply dump the database, open the dump with a text editor, replace all occurrences of "CREATE TABLE " with "CREATE TABLE dp_" and restore the database. It takes a couple of minutes to do.
-
3
-
I'm game. :) For databases larger than a GB, use a query tool (SQL Workbench, for example), select all the tables in the table list, copy the contents into column A of a spreadsheet (in my case, Calc) and put the following formula in column B: ="rename table " & A1 & " to dp_" & A1 & ";" Paste the formula from cell B1 into every other B column cell and the rename script appears in column B! Mar 17, 2010 at 23:05
-
as i said in my answer - an appropriate way to rename is to use RENAME TABLE (+ information_shema to retrieve tables names)– zerkmsMar 18, 2010 at 1:39
-
or you could just use eclipse and do a replace all for "CREATE TABLE " with "CREATE TABLE dp_" Jan 7, 2012 at 9:57
-
@Pooya of course, you're right, but I don't imagine it would be too much work to extend the approach to cover those, as well. It wouldn't be a 2 minute job like what I described above, but could probably be done in 10-15 minutes, sticking to the above principle of using a DDL/DML SQL script to implement the overall naming change. Nov 1, 2015 at 17:09
Just modded this slightly to account for situations where the prefix is also in the table name.
SET @database = "database_name";
SET @old_prefix = "old_prefix_";
SET @new_prefix = "new_prefix_";
SELECT
CONCAT(
"RENAME TABLE ",
TABLE_NAME,
" TO ",
CONCAT(@new_prefix, TRIM(LEADING @old_prefix FROM TABLE_NAME)),
';'
) AS "SQL" FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database;
If there's someone out there yet wondering how to do this (as it did not work form me the other options) you can run this (changing the first three variables for your values, of course):
SET @database = "database_name";
SET @old_prefix = "old_prefix_";
SET @new_prefix = "new_prefix_";
SELECT
concat(
"RENAME TABLE ",
TABLE_NAME,
" TO ",
replace(TABLE_NAME, @old_prefix, @new_prefix),
';'
) AS "SQL" FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database;
And then you will be prompted with a bunch of queries needed in order to change all the tables in your database. You simply have to copy that, run it and voilá!
SET @database = "Database-Name-Here";
SET @old_prefix = "wp_";
SET @new_prefix = "ab_";
SELECT
CONCAT(
"RENAME TABLE ",
@database,
".",
TABLE_NAME,
" TO ",
@database,
".",
CONCAT(@new_prefix, TRIM(LEADING @old_prefix FROM TABLE_NAME)),
';'
) AS "SQL" FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database;
Replace "Database-Name-Here" with the name of your database, "wp_" with the old prefix, and "ab_" with the new prefix. The preceding code will produce queries for the MySQL DB, also, the queries created by the above code are surrounded by single quotes, which must be replaced. However, if you are dealing with WordPress, you will need to do some more steps otherwise your site will be broken. Following the queries created by the aforementioned code, the following queries must be executed.
update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_capabilities' where meta_key = 'OLDPREFIX_capabilities';
update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_user_level' where meta_key = 'OLDPREFIX_user_level';
update NEWPREFIX_usermeta set meta_key = 'NEWPREFIX_autosave_draft_ids' where meta_key = 'OLDPREFIX_autosave_draft_ids';
update NEWPREFIX_options set option_name = 'NEWPREFIX_user_roles' where option_name = 'OLDPREFIX_user_roles'
Replace "NEWPREFIX" with your new prefix and "OLDPREFIX" with your old prefix.