简体   繁体   English

如何遍历数据库上的所有表以更新列

[英]How to loop through all the tables on a database to update columns

I'm trying to update a column (in this case, a date) that is present on most of the tables on my database. 我正在尝试更新数据库上大多数表中存在的列(在本例中为日期)。 Sadly, my database has more than 100 tables already created and full of information. 遗憾的是,我的数据库已经创建了100多个表,并且充满了信息。 Is there any way to loop through them and just use: 有没有办法循环它们只是使用:

UPDATE SET date = '2016-04-20' WHERE name = 'Example'

on the loop? 在循环?

One painless option would be to create a query which generates the UPDATE statements you want to run on all the tables: 一个无痛的选择是创建一个查询,该查询生成要在所有表上运行的UPDATE语句:

SELECT CONCAT('UPDATE ', a.table_name, ' SET date = "2016-04-20" WHERE name = "Example";')
FROM information_schema.tables a
WHERE a.table_schema = 'YourDBNameHere'

You can copy the output from this query, paste it in the query editor, and run it. 您可以复制此查询的输出,将其粘贴到查询编辑器中,然后运行它。

Update: 更新:

As @PaulSpiegel pointed out, the above solution might be inconvenient if one be using an editor such as HeidiSQL, because it would require manually copying each record in the result set. 正如@PaulSpiegel所指出的,如果使用HeidiSQL之类的编辑器,上述解决方案可能会很不方便,因为它需要手动复制结果集中的每条记录。 Employing a trick using GROUP_CONCAT() would give a single string containing every desired UPDATE query in it: 使用GROUP_CONCAT()使用技巧会给出一个包含每个所需UPDATE查询的字符串:

SELECT GROUP_CONCAT(t.query SEPARATOR '; ')
FROM
(
    SELECT CONCAT('UPDATE ', a.table_name,
                  ' SET date = "2016-04-20" WHERE name = "Example";') AS query,
        '1' AS id
    FROM information_schema.tables a
    WHERE a.table_schema = 'YourDBNameHere'
) t
GROUP BY t.id

You can use SHOW TABLES command to list all tables in database. 您可以使用SHOW TABLES命令列出数据库中的所有表。 Next you can check if column presented in table with SHOW COLUMNS command. 接下来,您可以使用SHOW COLUMNS命令检查表中是否显示列。 It can be used this way: 它可以这样使用:

SHOW COLUMNS FROM `table_name` LIKE `column_name`

If this query returns result, then column exists and you can perform UPDATE query on it. 如果此查询返回结果,则列存在,您可以对其执行UPDATE查询。

Update 更新

You can check this procedure on sqlfiddle . 您可以在sqlfiddle上查看此过程。

CREATE PROCEDURE UpdateTables (IN WhereColumn VARCHAR(10),
                               IN WhereValue VARCHAR(10),
                               IN UpdateColumn VARCHAR(10),
                               IN UpdateValue VARCHAR(10))
BEGIN
  DECLARE Finished BOOL DEFAULT FALSE;
  DECLARE TableName VARCHAR(10);

  DECLARE TablesCursor CURSOR FOR
    SELECT c1.TABLE_NAME
    FROM INFORMATION_SCHEMA.COLUMNS c1
      JOIN INFORMATION_SCHEMA.COLUMNS c2 ON (c1.TABLE_SCHEMA = c2.TABLE_SCHEMA AND c1.TABLE_NAME = c2.TABLE_NAME)
    WHERE c1.TABLE_SCHEMA = DATABASE()
      AND c1.COLUMN_NAME = WhereColumn
      AND c2.COLUMN_NAME = UpdateColumn;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;

  OPEN TablesCursor;

  MainLoop: LOOP
    FETCH TablesCursor INTO TableName;
    IF Finished THEN
      LEAVE MainLoop;
    END IF;

    SET @queryText = CONCAT('UPDATE ', TableName, ' SET ', UpdateColumn, '=', QUOTE(UpdateValue), ' WHERE ', WhereColumn, '=', QUOTE(WhereValue));
    PREPARE updateQuery FROM @queryText;
    EXECUTE updateQuery;
    DEALLOCATE PREPARE updateQuery;
  END LOOP;

  CLOSE TablesCursor;
END

This is just an example how to iterate through all tables in database and perform some action with them. 这只是一个如何遍历数据库中的所有表并使用它们执行某些操作的示例。 Procedure can be changed according to your needs. 程序可以根据您的需要进行更改。

Assuming you are using MySQL, You can use Stored Procedure . 假设您使用的是MySQL,则可以使用存储过程

This post is a very helpful. 这篇文章非常有帮助。

Mysql-loop-through-tables MYSQL-环通桌

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

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