Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

database

mysql

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. Is there any way to loop through them and just use:

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

on the loop?

like image 545
Myuu Avatar asked May 19 '16 04:05

Myuu


2 Answers

You can use SHOW TABLES command to list all tables in database. Next you can check if column presented in table with SHOW COLUMNS command. 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

You can check this procedure on 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.

like image 119
Andrew Avatar answered Nov 13 '22 21:11

Andrew


One painless option would be to create a query which generates the UPDATE statements you want to run on all the tables:

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. Employing a trick using GROUP_CONCAT() would give a single string containing every desired UPDATE query in it:

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
like image 20
Tim Biegeleisen Avatar answered Nov 13 '22 23:11

Tim Biegeleisen