我知道如何运行
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。
谢谢!
答案 0 :(得分:3)
使用以下存储过程::
DELIMITER $$
CREATE
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$$
DELIMITER;
将table_schema替换为table_schema名称。
答案 1 :(得分:0)
例如:
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
次出现。 希望有所帮助
答案 2 :(得分:0)
对@ 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_
重命名为blog_
(例如),则非常有用。将该命令的输出复制并粘贴到mysql shell或phpMyAdmin。
答案 3 :(得分:0)
我希望更新对我有用的内容,因为这篇文章很老了。我需要在表名之前和之后添加`标记。别忘了添加最后一个;因为这有时会引发语法错误。
{
"data": {
"0": 11,
"1": 1
},
"polarity_freq": {
"negative": 214,
"positive": 220
},
"sentiments_freq": {
"anger": 1,
"fear": 0,
"sad": 2,
"joy": 11
}
},