Below is some trics how to do this:
- simple dump & restore
mysqldump --add-drop-table --no-data dbname -u root -p -h dbhost > dbnamebackup.sql
mysql -u root -p -h dbhost < dbnamebackup.sql
- clever (root permission)
mysql -u root -p -h dbhost -e 'truncate table table;'
- sophistiocated (root permission )- using procedure
DELIMITER $$
CREATE PROCEDURE `delete_all_tables`(IN dbname varchar(255))
BEGIN
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmdtmp VARCHAR(2500);
DECLARE cmd CURSOR
FOR
SELECT CONCAT('TRUNCATE TABLE ',dbname,'.',table_name) FROM information_schema.tables WHERE table_schema = dbname;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN cmd;
REPEAT
FETCH cmd INTO cmdtmp;
SET @cmdtmp = cmdtmp;
PREPARE doit FROM @cmdtmp;
EXECUTE doit;
DROP PREPARE doit;
UNTIL done END REPEAT;
CLOSE cmd;
BEGIN
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmdtmp VARCHAR(2500);
DECLARE cmd CURSOR
FOR
SELECT CONCAT('TRUNCATE TABLE ',dbname,'.',table_name) FROM information_schema.tables WHERE table_schema = dbname;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN cmd;
REPEAT
FETCH cmd INTO cmdtmp;
SET @cmdtmp = cmdtmp;
PREPARE doit FROM @cmdtmp;
EXECUTE doit;
DROP PREPARE doit;
UNTIL done END REPEAT;
CLOSE cmd;
END; $$
DELIMITER ;
DELIMITER ;
How to use?
mysql> call mysql.delete_all_tables("databasename");