我知道怎么跑
RENAME TABLE onetable_test TO onetable;
但是有没有一种方法可以用一种模式重命名许多表并且不要写很多代码
RENAME TABLE onetable_test TO onetable;
RENAME TABLE twotable_test TO twitable;
RENAME TABLE threetable_test TO threetable;
...
我正在使用mysql。
谢谢!
我知道怎么跑
RENAME TABLE onetable_test TO onetable;
但是有没有一种方法可以用一种模式重命名许多表并且不要写很多代码
RENAME TABLE onetable_test TO onetable;
RENAME TABLE twotable_test TO twitable;
RENAME TABLE threetable_test TO threetable;
...
我正在使用mysql。
谢谢!
使用下面的存储过程::
DELIMITER $$
创造
PROCEDURE `Rename_Tables`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tableName VARCHAR(50);
DECLARE newTableName VARCHAR(70);
DECLARE t_query VARCHAR(500);
DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%table_test' AND table_schema='test' ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO tableName;
IF done THEN
LEAVE read_loop;
END IF;
SET newTableName = SUBSTRING(tableName, 1,LOCATE('_',tableName)-1);
SET t_query = CONCAT('RENAME TABLE ', tableName, ' TO ', newTableName);
SET @myQuery = t_query;
PREPARE stmt FROM @myQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
END$$
分隔符;
将 table_schema 替换为您的 table_schema 名称。
例如:
select group_concat(v.name SEPARATOR ' ')
from (
select concat('rename table ', t.table_name, ' to ', substring(reverse(v.name), instr(reverse(v.name), '_') + 1, length(v.name)) name
from information_schema.tables t
where
table_schema = 'put_your_table_schema'
and table_name like '%_test'
) v;
substring(reverse.....)
而不是简单的replace(v.name, '_test', '')
使用,因为我们需要确保只替换_test
字符串末尾的出现。希望能帮助到你
我想更新对我有用的东西,因为这篇文章很老了。我需要在表名之前和之后添加 ` 标记。并且不要忘记添加最后一个;因为这有时会引发语法错误。
SET group_concat_max_len=5000;
SELECT group_concat(v.name separator '; ')
FROM (
SELECT concat('RENAME TABLE `', t.table_name, '` TO `', replace(t.table_name, 'wp_', 'wplocal_'), '`') name
FROM information_schema.tables t
WHERE table_name like 'wp_%'
) v;
对@ravnur 的回答稍作修改:
SET group_concat_max_len=5000;
SELECT group_concat(v.name separator '; ')
FROM (
SELECT concat('RENAME TABLE ', t.table_name, ' TO ', replace(t.table_name, 'wp_', 'blog_')) name
FROM information_schema.tables t
WHERE table_name like 'wp_%'
) v;
当您想要将您的 Wordpress 表格从默认前缀重命名为wp_
to blog_
(例如)时很有用。将该命令的输出复制并粘贴到 mysql shell 或 phpMyAdmin 中。