SQL 重命名表命令

发布于 2024-08-29 04:23:29 字数 596 浏览 6 评论 0原文

我可以运行 RENAME TABLE Student TO Student_new ;
该命令是相同的并且易于遵循。

有没有一种方法可以用简单的命令重命名很多表。假设所有表都属于同一个数据库名称。

我不需要写很多代码,如下所示?

RENAME TABLE pre_access TO pre_new_access; 
RENAME TABLE pre_activities TO pre_new_activities;
RENAME TABLE pre_activityapplies TO pre_new_activityapplies;
RENAME TABLE pre_adminactions TO pre_new_adminactions;
RENAME TABLE pre_admincustom TO pre_new_admincustom;
RENAME TABLE pre_admingroups TO pre_new_admingroups;
RENAME TABLE pre_adminnotes TO pre_new_adminnotes;
...

(还有那么多表需要重命名)

更新:MySQL已使用。

I can run RENAME TABLE student TO student_new
;

The command is same and easy to follow.

Is there a methods to rename a lot of tables in simple command. Assume all the tables belong to the same DB name.

I don't need write a lot of code as below?

RENAME TABLE pre_access TO pre_new_access; 
RENAME TABLE pre_activities TO pre_new_activities;
RENAME TABLE pre_activityapplies TO pre_new_activityapplies;
RENAME TABLE pre_adminactions TO pre_new_adminactions;
RENAME TABLE pre_admincustom TO pre_new_admincustom;
RENAME TABLE pre_admingroups TO pre_new_admingroups;
RENAME TABLE pre_adminnotes TO pre_new_adminnotes;
...

(there are still so many tables need to be renamed)

Update: MySQL Used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

菩提树下叶撕阳。 2024-09-05 04:23:29

根据您的评论,假设您使用 MySQL,您应该能够从包含表列表的 information_schema.tables 中“选择”必要的语句。

your_schema 替换为您的架构名称,并在执行之前检查所选行。

Select Concat( 'RENAME TABLE ', table_name, ' TO ', 'new_', table_name, ';' )
From information_schema.tables
Where table_schema = 'your_schema';

回报

RENAME TABLE c_data TO new_c_data;
RENAME TABLE c_main TO new_c_main;
...

Assuming from your comment that you use MySQL, you should be able to "select" the necessary statements from information_schema.tables which contains a list of your tables.

Replace your_schema by your schema-name and check the selected rows before executing them.

Select Concat( 'RENAME TABLE ', table_name, ' TO ', 'new_', table_name, ';' )
From information_schema.tables
Where table_schema = 'your_schema';

returns

RENAME TABLE c_data TO new_c_data;
RENAME TABLE c_main TO new_c_main;
...
淡墨 2024-09-05 04:23:29

您始终可以生成一个简单的 PHP 脚本,该脚本循环访问数据库并根据 REGEX 表达式重命名它们。

You could always generate a simple PHP script that loops through the databases and renames them based on a REGEX expression.

少女情怀诗 2024-09-05 04:23:29

您没有提及您正在使用的数据库,这完全依赖于数据库(某些 DBMS 甚至不允许您重命名表)。但是,我不知道有任何 DBMS 系统允许在 RENAME 命令中使用通配符,所以是的,您可能必须单独发送每个命令。

You don't mention what database you're using, and this is completely database dependent (some DBMSes don't even allow you to rename tables). However, I don't know of any DBMS system off the top of my head that allows wildcards in a RENAME command so yes, you will probably have to send each command separately.

壹場煙雨 2024-09-05 04:23:29

对于 mysql 数据库“测试”应该有效:

echo "show tables"|mysql test|perl -ne 'if(/pre_(\w+)/){print "rename table pre_$1 to pre_new_$1;\n"}'|mysql test

for mysql database "test" should work:

echo "show tables"|mysql test|perl -ne 'if(/pre_(\w+)/){print "rename table pre_$1 to pre_new_$1;\n"}'|mysql test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文