I have 100 tables, 40,000 rows in each table. I want to go into MySQL and delete all rows from all tables.
...in 1 statement, if p
Use MySQL Workbench to get the Entity Relationship Diagram of your database ( in Workbench : Database -> Reverse Engineer ). Then drop the database. Then create database using Workbench ( Database -> Forward Engineer ).
Just a guess...try it
TRUNCATE TABLE *;
Or
TRUNCATE TABLE table1, table2, table3;
Or
DELETE FROM table1, table2, table3 WHERE 1=1;
DELIMITER $$
DROP PROCEDURE IF EXISTS `cleanAllTables`$$
CREATE DEFINER=`root`@`%` PROCEDURE `cleanAllTables`()
BEGIN
DROP Temporary TABLE IF EXISTS AllTables;
Create Temporary Table AllTables (
SELECT @curRow := @curRow + 1 AS row_number
, table_name
FROM INFORMATION_SCHEMA.tables s
JOIN (SELECT @curRow := 0
) r
WHERE s.table_schema = 'databasename');
set @countOfAllTables = (select count(*) from AllTables);
set @c = 1;
WHILE @c<=@countOfAllTables DO
set @table_name = (select table_name from AllTables where row_number = @c);
set @stmt = concat( 'Truncate Table ', @table_name);
Prepare stmt from @stmt;
Execute stmt;
SET @c = @c + 1 ;
END WHILE ;
END$$
DELIMITER ;
This two line Script is the best... try it
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO
The most robust way to clear all tables in a database was submitted by kostanos in How to remove all MySQL tables from the command-line without DROP database permissions?
Since the code below clears all tables in the current database you might want to select a different database before you proceed.
USE DATABASE_YOU_WANT_TO_CLEAR;
Following snippet will remove data from the tables even in presence of foreign key constraints. You might also want to double check the list of tables before actual cleanup, just in case if you forgot to select proper database.
-- save current foreign key settings and disable foreign key checks
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET GROUP_CONCAT_MAX_LEN=32768; -- you never know how people name their tables
SET @tables = NULL;
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
FROM information_schema.tables
WHERE table_schema = (SELECT DATABASE()); -- operates on the current DB
SELECT IFNULL(@tables,'dummy') INTO @tables; -- avoid error if there are no tables
-- At this point you might want to double check the list of tables
-- to be cleared. Run SELECT @tables; to do so.
SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
I had to restore a backup which was missing the create statements. So I restored an older backup but I had to get rid of the data. This oneliner helped me truncate everything.
mysql -u user -ppass -NBe "SELECT 'SET foreign_key_checks = 0;' UNION SELECT CONCAT('TRUNCATE TABLE myschema.', table_name, ';') FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'myschema' AND table_type = 'BASE TABLE'" | mysql -u user -ppass
For better readability again with line breaks:
mysql -u user -ppass -NBe "SELECT 'SET foreign_key_checks = 0;'
UNION SELECT CONCAT('TRUNCATE TABLE myschema.', table_name, ';')
FROM INFORMATION_SCHEMA.tables
WHERE table_schema = 'myschema' AND table_type = 'BASE TABLE'"
| mysql -u user -ppass
be sure to use the string without line breaks and replace myschema
with your table schema and set the correct login credentials or host for mysql process.