简体   繁体   English

MySQl批量重命名,更改,更新

[英]MySQl Bulk Rename, Alter , Update

I have a MySQl database with more than 200 tables. 我有一个MySQl数据库,其中包含200多个表。 I want to do following on ALL tables in this database. 我想对该数据库中的所有表执行以下操作。

  1. Update all table name by adding a constant to the name 通过向名称添加常量来更新所有表名称
  2. Add column (Alter table) to each table 将列(更改表)添加到每个表
  3. Update each table to set newly added column 更新每个表以设置新添加的列

Can someone please suggest an efficient way of doing this 有人可以建议一种有效的方法吗

Thanks bhim 谢谢比姆

You need to write a couple of SQL statements that will generate the rename / add column SQL statements. 您需要编写几个SQL语句,这些语句将生成重命名/添加列SQL语句。

Then you can run the SQL Statements. 然后,您可以运行SQL语句。

You haven't provided table names, or schemas, etc. so I can give guidance but not exact results. 您尚未提供表名或架构等,因此我可以提供指导,但不能提供确切的结果。

So assuming your "adding a constant to the name" is prefixing "const_" to it, you could do something like: 因此,假设您在“名称中添加常量”的前缀是“ const_”,则可以执行以下操作:

SELECT 'RENAME TABLE ''' || table_name || ''' TO ''const_' || table_name || ''' FROM information_schema.tables WHERE table_catalog = 'YourCatalog' and table_schema = 'YourSchema';

This would give you the rename table command as the output, which you could pick up and put in a text editor to tidy up. 这将为您提供重命名表命令作为输出,您可以选择并放入文本编辑器中进行整理。

You'll need to execute a few queries against INFORMATION_SCHEMA.tables to figure out the right filter to get the right criteria for the tables list. 您需要对INFORMATION_SCHEMA.tables执行一些查询,以找出正确的过滤器,以获取表列表的正确条件。

And you can do similar for the Add column statement. 您可以对Add列语句执行类似的操作。

Some useful references: 一些有用的参考:

Information schema: https://dev.mysql.com/doc/refman/5.7/en/tables-table.html 信息模式: https : //dev.mysql.com/doc/refman/5.7/en/tables-table.html

Rename a table: https://blog.marceloaltmann.com/en-how-to-rename-table-in-mysql-pt-como-renomear-tabelas-no-mysql/ 重命名表格: https//blog.marceloaltmann.com/en-how-to-rename-table-in-mysql-pt-como-renomear-tabelas-no-mysql/

Add a column: http://www.mysqltutorial.org/mysql-add-column/ 添加一列: http : //www.mysqltutorial.org/mysql-add-column/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM