搜索在mysql中一次更改多个表的简单方法

发布于 2024-09-09 19:29:05 字数 381 浏览 7 评论 0原文

我有很多以某个前缀开头的表,

我想更改此表,

执行此操作的简单方法是什么(而不是运行所有表)

我的意思是:

ALTER TABLE  LIKE tablenameprefix% ADD INDEX `NewIndex1` (`field`);

我该怎么做?

谢谢

编辑:

我可以做一种不在存储过程中的循环吗? 通过选择表的名称

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'tableprefix%'

i have a lot of tables that start with some prefix ,

and i want to alter this tables

what is the simple way to do this (instead run over all tables)

i mean something like :

ALTER TABLE  LIKE tablenameprefix% ADD INDEX `NewIndex1` (`field`);

how can i do this ?

thanks

EDIT :

can i do a kind of loop not in stored procedure ?
by select the names of tables from

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'tableprefix%'

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

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

发布评论

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

评论(5

五里雾 2024-09-16 19:29:05

你不能。您可以做的是编写一个存储过程,枚举所有查找您的前缀的表并执行必要的更改。

You can't. What you could do though is write a stored procedure that enumerates all tables looking for your prefix and performs the necessary changes.

静水深流 2024-09-16 19:29:05

由于 ALTER TABLE 语法不允许多个表名,因此您无法执行此操作。您需要依次浏览所有表:

ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification]

链接:http: //dev.mysql.com/doc/refman/5.0/en/alter-table.html

Given that ALTER TABLE syntax doesn't allow multiple table names, you cannot do this. You need to go through all tables in turn:

ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification]

Link: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

喜爱纠缠 2024-09-16 19:29:05

当我想将多个表的引擎从 MyISAM 更改为 InnoDB 时,我没有编写循环,而是制作了完整的数据库转储并在文本编辑器中打开它。在文本编辑器中,我只是将所有 MyISAM 单词更改为 InnoDB。

我知道这不是正确的解决方案,但对我来说,这比为此编写例程更容易。

When I wanted to change multiple table's engine from MyISAM to InnoDB, instead of writing a loop I just made a full DB dump and opened it in a text editor. In the text editor I just simply changed all MyISAM words to InnoDB.

I know that this ain't proper solution but for me it was easier then writing a routine for this.

贪了杯 2024-09-16 19:29:05

You would have to write a loop, according to the documentation you just specify the table name.

半城柳色半声笛 2024-09-16 19:29:05

我犯了一个愚蠢的错误,将所有 WordPress 表与我的产品表放在一起,幸运的是所有 Wordpress 表都以 wp_ 前缀开头,而我所有其他产品表都没有此 wp_ 前缀。

我创建了另一个名为 wordpress 的数据库,现在我想将所有以 wp_ 开头的表移动到该数据库。

这就是我所做的:

SELECT CONCAT('ALTER TABLE olddb.', table_name, ' RENAME wordpress.', table_name, ';')
  FROM information_schema.tables
  WHERE table_schema='olddb' AND table_name LIKE 'wp%'
  INTO OUTFILE '/tmp/move_to_wordpress';
SOURCE /tmp/move_to_wordpress;

就是这样。

I made a stupid mistake of putting all Wordpress tables with my product tables, fortunately all Wordpress tables start with a wp_ prefix and all my other product tables have no this wp_ prefix.

I created another database named wordpress, now I want to move all tables start with wp_ to that database.

Here is what I did:

SELECT CONCAT('ALTER TABLE olddb.', table_name, ' RENAME wordpress.', table_name, ';')
  FROM information_schema.tables
  WHERE table_schema='olddb' AND table_name LIKE 'wp%'
  INTO OUTFILE '/tmp/move_to_wordpress';
SOURCE /tmp/move_to_wordpress;

That's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文