ESEMPIO: ho più di 30 tabelle che iniziano con un prefisso "dp_" e circa 12 che iniziano con "ex_".
DOMANDA: Come posso eliminare tutte le tabelle iniziando "dp_" in una query?
ESEMPIO: ho più di 30 tabelle che iniziano con un prefisso "dp_" e circa 12 che iniziano con "ex_".
DOMANDA: Come posso eliminare tutte le tabelle iniziando "dp_" in una query?
Risposte:
Ecco una procedura memorizzata che accetta il database e la stringa di prefisso come parametri:
DELIMITER $$
DROP PROCEDURE DropTablesWithPrefix $$
CREATE PROCEDURE DropTablesWithPrefix(db VARCHAR(64),prfx VARCHAR(20))
StoredProcedure:BEGIN
DECLARE ndx,maxidx INT;
DECLARE giventable,SQLSTMT VARCHAR(500);
CREATE TABLE TableZapList
(
droptablesql VARCHAR(512),
idx INT NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=MyISAM;
INSERT INTO TableZapList (droptablesql)
SELECT CONCAT('DROP TABLE ',table_schema,'.',table_name)
FROM information_schema.tables
WHERE table_schema = db AND SUBSTR(table_name,LENGTH(prfx)) = prfx;
SELECT COUNT(1) INTO maxid FROM TableZapList;
IF maxid = 0 THEN
LEAVE StoredProcedure;
END IF;<BR>
SET ndx = 0;
WHILE ndx < maxid DO
SET ndx = ndx + 1;
SELECT droptablesql INTO SQLSTMT FROM TableZapList WHERE idx = ndx;
SET @sql = SQLSTMT;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END WHILE;<BR>
SELECT droptablesql FROM TableZapList;
DROP TABLE TableZapList;
END;
DELIMITER ;
AGGIORNAMENTO 2011-07-12 14:55 EDT
Ho solo pensato a un modo più semplice e pulito. Invece di utilizzare una procedura memorizzata, è sufficiente utilizzare la funzione GROUP_CONCAT per raccogliere tutti i tavoli da zappare. Quindi componi in una singola query:
Ecco una query per eliminare tutte le tabelle che iniziano con wp_pol nel database corrente:
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';')
FROM information_schema.tables
WHERE table_schema=database()
AND table_name like 'wp_pol%';
La prossima cosa da fare è archiviarne il risultato
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';')
INTO @dropcmd
FROM information_schema.tables
WHERE table_schema=database()
AND table_name like 'wp_pol%';
L'ultima cosa è eseguire l'SQL dinamico usando questi tre (3) comandi:
PREPARE s1 FROM @dropcmd;
EXECUTE s1;
DEALLOCATE PREPARE s1;
Ecco una dimostrazione che utilizza MySQL 5.5.12 in Windows che funziona:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.12 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
MySQL (Current test) :: use garbage
Database changed
MySQL (Current garbage) :: show tables;
+------------------------------+
| Tables_in_garbage |
+------------------------------+
| datas |
| rolando |
| wp_commentmeta |
| wp_comments |
| wp_contact_form_7 |
| wp_links |
| wp_most_read_hits |
| wp_options |
| wp_pollsa |
| wp_pollsip |
| wp_pollsq |
| wp_postmeta |
| wp_posts |
| wp_posts_idtracker |
| wp_tantan_wordpress_s3_cache |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
| wp_w3tc_cdn_queue |
+------------------------------+
21 rows in set (0.00 sec)
MySQL (Current garbage) :: SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @dropcmd FROM information_schema.tables WHERE table_schema=database() AND table_name like 'wp_pol%';
Query OK, 1 row affected (0.00 sec)
MySQL (Current garbage) :: SELECT @dropcmd;
+--------------------------------------------------------------------+
| @dropcmd |
+--------------------------------------------------------------------+
| DROP TABLE garbage.wp_pollsa,garbage.wp_pollsip,garbage.wp_pollsq; |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)
MySQL (Current garbage) :: PREPARE s1 FROM @dropcmd; EXECUTE s1; DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
MySQL (Current garbage) :: show tables;
+------------------------------+
| Tables_in_garbage |
+------------------------------+
| datas |
| rolando |
| wp_commentmeta |
| wp_comments |
| wp_contact_form_7 |
| wp_links |
| wp_most_read_hits |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_posts_idtracker |
| wp_tantan_wordpress_s3_cache |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
| wp_w3tc_cdn_queue |
+------------------------------+
18 rows in set (0.00 sec)
MySQL (Current garbage) ::
Provaci !!!
SUBSTRcondizione INSERTdovrebbe essere: SUBSTR(table_name, 1, LENGTH(prfx)) = prfxquindi funziona benissimo.
Innanzitutto, genera uno script per farlo al prompt di Unix:
$ echo "select concat('drop table ', table_name, ';') from information_schema.tables where table_name like 'prefix_%';" |mysql --user=root --password=blah --batch >drop.sql
Sostituisci prefisso con il tuo. L' --batchopzione elimina la formattazione elaborata che MySQL esegue per impostazione predefinita in modo da poter produrre uno script SQL eseguibile.
Rivedi lo script e, se sembra OK, esegui drop.sqlal mysql>prompt.
È necessario interrogare le tabelle di sistema per quei nomi di tabella e creare una stringa per eseguirla in modo dinamico. In SQL Server farei qualcosa del tipo:
declare @x varchar(max), @enter char(2);
select @x = '', @enter = char(13)+char(10);
Select @x = @x + 'drop table ' + table_name + ';' + @enter
from information_schema.tables
where table_name like '%accounting%'
print @x
execute (@x);
Ora devi trovare la tabella di sistema corrispondente in MySQL.
Per rispondere alla tua domanda, no. Non in MySQL usando un singolo comando / query. Dovrai incatenare i comandi.
Tuttavia, se vuoi raggiungere il tuo obiettivo, ecco un modo:
Da uno script bash qualcosa del tipo:
#/bin/bash
TABLES=`mysql -s -e "SELECT CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) AS T FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'dp_%'" -u user -p`
for i in $TABLES;
do
echo "DROP TABLE $i;" >> drops.sql ;
done
cat drops.sql
Quindi rivedere il file drops.sql. Se è tutto a posto, crea un BACKUP , quindi ...
mysql -u username -p -v --show-warnings < drops.sql
È sempre possibile utilizzare un AMMINISTRATORE DI DATABASE come Navicat e quel tipo di problemi scompare con una semplice selezione ed eliminazione.