rename table to 'NOW() + old_table_name'

爷,独闯天下 提交于 2019-12-23 13:15:10

问题


is it possible somehow to use the NOW() function or something similar in MYSQL rename table()?

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'


回答1:


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;

The curdate() function returns the current date as string in the format yyyy-MM-dd.

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


来源:https://stackoverflow.com/questions/1741008/rename-table-to-now-old-table-name

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