How can I delete the contents of all tables in my database in phpMyAdmin without dropping the database?

后端 未结 10 2498
野趣味
野趣味 2021-02-13 03:22

How can I empty the contents of all tables in my database in phpMyAdmin without dropping any of the tables in the database?

Since I do this several times an hour while i

相关标签:
10条回答
  • 2021-02-13 03:39

    Create a SQL script with multiple DELETE statements (one for each table) and execute it.

    Get into phpMyAdmin and select the database that you want. Select the SQL tab and paste the SQL script into the window. Hit Go.

    Look here too:

    Drop all tables from a MySQL Database without deletion

    0 讨论(0)
  • 2021-02-13 03:40

    You could export the table structures only like this:

    mysqldump -u root -p --no-data mydb > backup_info.sql
    

    then drop the whole database, recreate it and restore from the backup.

    Easy enough to script the whole thing.

    0 讨论(0)
  • 2021-02-13 03:46

    you could start with this query

    SELECT T.*
    FROM INFORMATION_SCHEMA.tables T
    WHERE T.table_type = 'BASE TABLE'
    

    And iterate through those results to build a dynamic SQL string to the tune of 'DELETE FROM ' + T.Table_Name

    0 讨论(0)
  • 2021-02-13 03:46

    Unfortunately, there is no TRUNCATE DATABASE or equivalent. With that said, you could probably use some kind of stored procedure that go through all tables in your database and truncate them. I found something of that kind here, but I don't know whether it works. (You should probably read the comment discussion too)

    0 讨论(0)
  • 2021-02-13 03:46

    The TRUNCATE TABLE statement will empty a table completely (MySql 3.23 and above).

    You can create a script that you can reuse for a particular database. It's not generic but saves you from creating a new database + login each time you want to start with a fresh db.

    Here's how to create a script yourself: In your webbrowser go to phpMyAdmin and select the database. Click the SQL tab and execute 'SHOW TABLES'. Choose the printerfriendly format and copy the contents to a text editor.

    Now for each table name you re-write it in the format:

    TRUNCATE TABLE <tablename>;
    

    note: You can use a regular expression if you have many tables

    0 讨论(0)
  • 2021-02-13 03:50
    drop procedure if exists truncate_tables;
    
    delimiter #
    create procedure truncate_tables()
    begin
     declare tab_name varchar(64);
     declare done tinyint unsigned default 0;
    
     declare table_cur cursor for select t.table_name
     from 
      information_schema.schemata s
      inner join information_schema.tables t on s.schema_name = t.table_schema
     where
       s.schema_name = database() and t.table_type = 'BASE TABLE';
    
     declare continue handler for not found set done = 1;
    
     open table_cur;
     repeat
       fetch table_cur into tab_name;
       set @cmd = concat('truncate table ', tab_name);
    
       prepare stmt from @cmd;
       execute stmt;
     until done end repeat;
    
     close table_cur;
    end #
    
    0 讨论(0)
提交回复
热议问题