Skip to content

Commit 7a97314

Browse files
committed
Automatic Repair Procedure v0.2
-Renamed directory and script file name to match it's purpose. -Generalization of the script. -Insertion of README.md file. -Version 0.2 of the script.
1 parent db546bc commit 7a97314

File tree

2 files changed

+122
-40
lines changed

2 files changed

+122
-40
lines changed

Automatic_Repair_Procedure/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Automatic Repair Procedure - v0.2
2+
3+
Languages:
4+
5+
* [English](##English)
6+
* [Português](##Português)
7+
8+
## English
9+
10+
**IMPORTANT: I AM NOT RESPONSIBLE FOR ANY DATA OR DATABASE WHICH HAS BEEN INCORRECTLY REPAIRED OR CORRUPTED COMPLETELY!**
11+
12+
This is a simple procedure that I used to repair a large database with hundreds of tables. Doing it manually would be absolutely exhaustive and insane, so keeping that in mind, I've created a procedure that automates this process by taking the name of every table in the especific database that you want to restore and runs the restore command.
13+
14+
Although it's simple it uses the concept of Dynamic SQL so that it is possible to execute queries through the Prepared Statements, because it isn't possible to pass the name of tables through normal queries as parameters to a system function. The CHECK TABLE function is not yet supported by the Prepared Statements in **MySQL Server 8**.
15+
16+
*Any error or wrong information please contact.*
17+
18+
## Português
19+
20+
**IMPORTANTE: NÃO ME RESPONSABILIZO POR QUALQUER DADO OU BANCO DE DADOS QUE FORA REPARADO INCORRETAMENTE OU SE CORROMPEU POR COMPLETO!**
21+
22+
Essa é um procedimento simples que utilizei para reparar um banco de dados grande com centenas de tabelas. Fazê-lo manualmente seria absolutamente exaustivo e insano, então tendo isso em mente, criei um procedimento que automatiza esse processo pegando o nome de cada tabela no banco de dados especificado que você quer restaurar e executa o comando de restauração.
23+
24+
Apesar de ser simples ele utiliza o conceito de SQL Dinâmico para que seja possível executar as consultas através das *Prepared Statements*, pois não é possível passar o nome de tabelas através de consultas normais para uma função do sistema. A função de checar as tabelas (CHECK TABLE) ainda não é suportada pelas *Prepared Statements* no **MySQL Server 8**.
25+
26+
*Qualquer erro ou informação incorreta por favor contatar.*
27+
28+
## References
29+
30+
REPAIR TABLE:
31+
32+
* [Repair all tables in one go - Stack Overflow](https://stackoverflow.com/questions/4582832/repair-all-tables-in-one-go)
33+
* [REPAIR TABLE Syntax- MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/repair-table.html)
34+
35+
TEMPORARY TABLE:
36+
37+
* [TEMPORARY TABLE - MySQLTutorial](http://www.mysqltutorial.org/mysql-temporary-table/)
38+
* [CREATE TEMPORARY TABLE Syntax- MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html)
39+
* [How to create Temporary Table with SELECT INTO - Stack Overflow](https://stackoverflow.com/questions/11491240/how-to-create-temp-table-with-select-into-temptable-from-cte-query)
40+
41+
VARIABLES:
42+
43+
* [How to set variable from a SQL Query](https://stackoverflow.com/questions/3974683/how-to-set-variable-from-a-sql-query)
44+
* [Local Variable DECLARE Syntax- MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html)
45+
* [Use a variable for table name - Stack Overflow](https://stackoverflow.com/questions/2754423/use-a-variable-for-table-name-in-mysql-sproc)
46+
47+
WHILE LOOP:
48+
49+
* [WHILE LOOP Syntax- MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/while.html)
50+
* [INSERT with WHILE LOOP - Stack Overflow](https://stackoverflow.com/questions/26981901/mysql-insert-with-while-loop/26983030)
51+
* [SELECT INTO - W3Schools](https://www.w3schools.com/sql/sql_select_into.asp)
52+
* [Looping through all tables on a database - Stack Overflow](https://stackoverflow.com/questions/37313983/how-to-loop-through-all-the-tables-on-a-database-to-update-columns)
53+
54+
AUTO_INCREMENT:
55+
56+
* [How to generate Auto-increment field in SELECT query - Stack Overflow](https://stackoverflow.com/questions/16555454/how-to-generate-auto-increment-field-in-select-query)
57+
58+
PARAMETERS:
59+
60+
* [Passing table names as parameters - Stack Overflow](https://stackoverflow.com/questions/25124034/passing-a-table-name-as-a-parameter)
61+
* [Passing table name as a parameter in stored procedure - SQL SERVER CENTRAL](https://www.sqlservercentral.com/Forums/485395/pass-table-name-as-a-parameter-in-stored-procedure)
62+
63+
PROCEDURE:
64+
65+
* [CREATE PROCEDURE Syntax- MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html)
66+
67+
PREPARED STATEMENTS:
68+
69+
* [Prepared SQL Statement Syntax - MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html)
70+
* [PREPARE Syntax - MySQL Documentation](https://dev.mysql.com/doc/refman/8.0/en/prepare.html)

Automatic_Repair/query_repair_all.sql renamed to Automatic_Repair_Procedure/procedure_repair_all.sql

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/* DELETES THE PROCEDURE IF IT ALREADY EXISTS */
22
DROP PROCEDURE REPAIR_ALL;
33

4-
/* CREATES THE PROCEDURE */
4+
/* CREATES THE PROCEDURE WITHIN THE DATABASE YOU WANT TO REPAIR */
55
DELIMITER $$
66
CREATE PROCEDURE REPAIR_ALL()
77
BEGIN
88

9+
/* DROP TEMPORARY TABLE DEFINED AS TEMP TO PREVENT ERRORS */
10+
DROP TABLE Temp;
11+
12+
/* DECLARATION OF VARIABLES */
913
DECLARE i int DEFAULT 1;
1014
DECLARE maximum int DEFAULT 0;
1115

@@ -18,6 +22,7 @@ BEGIN
1822

1923
WHILE i <= maximum DO
2024

25+
/* GET EACH TABLE NAME IN ORDER */
2126
SET temp_name = (SELECT Temp.table_name FROM Temp WHERE n = i);
2227

2328
/* USE OF DYNAMIC SQL TO REPAIR THE TABLES */
@@ -36,44 +41,12 @@ END $$
3641
DELIMITER ;
3742
/* END OF SCRIPT */
3843

39-
/* REFERENCES */
40-
https://stackoverflow.com/questions/37313983/how-to-loop-through-all-the-tables-on-a-database-to-update-columns
41-
42-
https://stackoverflow.com/questions/3974683/how-to-set-variable-from-a-sql-query
43-
44-
https://stackoverflow.com/questions/19891594/in-sql-server-how-to-create-while-loop-in-select#
45-
46-
https://dev.mysql.com/doc/refman/8.0/en/while.html
47-
48-
https://dev.mysql.com/doc/refman/8.0/en/repair-table.html
49-
50-
https://www.w3schools.com/sql/sql_select_into.asp
51-
52-
https://stackoverflow.com/questions/16555454/how-to-generate-auto-increment-field-in-select-query
53-
54-
https://stackoverflow.com/questions/11491240/how-to-create-temp-table-with-select-into-temptable-from-cte-query
55-
56-
http://www.mysqltutorial.org/mysql-temporary-table/
57-
58-
https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html
59-
60-
https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
61-
62-
https://stackoverflow.com/questions/26981901/mysql-insert-with-while-loop/26983030
63-
64-
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
65-
66-
https://stackoverflow.com/questions/25124034/passing-a-table-name-as-a-parameter
67-
68-
https://www.sqlservercentral.com/Forums/485395/pass-table-name-as-a-parameter-in-stored-procedure
69-
70-
https://stackoverflow.com/questions/2754423/use-a-variable-for-table-name-in-mysql-sproc
71-
72-
https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
73-
74-
https://dev.mysql.com/doc/refman/8.0/en/prepare.html
75-
76-
https://stackoverflow.com/questions/4582832/repair-all-tables-in-one-go
44+
/*
45+
REMOVE THE PROCEDURE ONCE IT WON'T BE USED ANYMORE
46+
IF ANY ERRORS HAVE OCCURRED THAT WITHIN THE EXECUTION,
47+
CHECK THEM AND TRY TO RUN AGAIN.
48+
*/
49+
DROP PROCEDURE REPAIR_ALL;
7750

7851
/* CHECK TABLE PREPARED STATEMENT NOT SUPPORTED BY MYSQL YET */
7952
SET @sql_query = (CONCAT('CHECK TABLE ', table_name ) from information_schema.tables where table_schema = 'mydatabase');
@@ -112,4 +85,43 @@ WHERE a.table_schema = 'mydatabase';
11285

11386
/* QUERY FOR INSERTING AN IDENTIFIER FOR EACH TABLE NAME OF DATABASE*/
11487
SELECT @n := @n + 1 n,a.table_name FROM information_schema.tables a ,
115-
(SELECT @n := 0) m WHERE a.table_schema = 'mydatabase';
88+
(SELECT @n := 0) m WHERE a.table_schema = 'mydatabase';
89+
90+
/* REFERENCES */
91+
/*
92+
https://stackoverflow.com/questions/37313983/how-to-loop-through-all-the-tables-on-a-database-to-update-columns
93+
94+
https://stackoverflow.com/questions/3974683/how-to-set-variable-from-a-sql-query
95+
96+
https://dev.mysql.com/doc/refman/8.0/en/while.html
97+
98+
https://dev.mysql.com/doc/refman/8.0/en/repair-table.html
99+
100+
https://www.w3schools.com/sql/sql_select_into.asp
101+
102+
https://stackoverflow.com/questions/16555454/how-to-generate-auto-increment-field-in-select-query
103+
104+
https://stackoverflow.com/questions/11491240/how-to-create-temp-table-with-select-into-temptable-from-cte-query
105+
106+
http://www.mysqltutorial.org/mysql-temporary-table/
107+
108+
https://dev.mysql.com/doc/refman/8.0/en/create-temporary-table.html
109+
110+
https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
111+
112+
https://stackoverflow.com/questions/26981901/mysql-insert-with-while-loop/26983030
113+
114+
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
115+
116+
https://stackoverflow.com/questions/25124034/passing-a-table-name-as-a-parameter
117+
118+
https://www.sqlservercentral.com/Forums/485395/pass-table-name-as-a-parameter-in-stored-procedure
119+
120+
https://stackoverflow.com/questions/2754423/use-a-variable-for-table-name-in-mysql-sproc
121+
122+
https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
123+
124+
https://dev.mysql.com/doc/refman/8.0/en/prepare.html
125+
126+
https://stackoverflow.com/questions/4582832/repair-all-tables-in-one-go
127+
*/

0 commit comments

Comments
 (0)