将表重命名为'NOW()+ old_table_name' [英] rename table to 'NOW() + old_table_name'
问题描述
是否可以通过某种方式使用NOW()函数或MYSQL重命名table()中的类似方法?
is it possible somehow to use the NOW() function or something similar in MYSQL rename table()?
我需要这个的原因是,我们宁愿先重命名,然后重命名为old_ date-table-was-taken-ouf-of-use _ table_name,而不是立即丢弃旧表,所以当我们实际上删除它我们知道表已经不可用"多久了
the reason i need this is because instead of dropping old tables straight away we prefer to first rename then to old_ date-table-was-taken-ouf-of-use_ table_name so when we actualy delete it we know how long the table has been 'unavailable'
推荐答案
您可以创建动态SQL语句并执行该语句:
You can create a dynamic SQL statement and execute that:
SET @tablename = 'MyTable';
SELECT @query := CONCAT('RENAME TABLE `', @tablename, '` TO `',
CURDATE(), @tablename, '`');
PREPARE STMT FROM @query;
EXECUTE STMT;
curdate()
函数以字符串形式以yyyy-MM-dd
的形式返回当前日期.
The curdate()
function returns the current date as string in the format yyyy-MM-dd
.
P.S.您不能从查询浏览器中执行这样的多行语句,但是可以将它们放入文件(例如称为commandfile.sql
)并按以下方式运行它们:
P.S. You can't execute multi-line statements like this from the Query Browser, but you can put them into a file (for example called commandfile.sql
) and run them like:
mysql -u <user> -p<password> <dbname> < commandfile.sql
这篇关于将表重命名为'NOW()+ old_table_name'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!