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

后端 未结 10 1372
予麋鹿
予麋鹿 2021-02-13 03:32

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:41

    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:41

    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:43

    IMHO the easiest solution is:

    • In phpmyadmin select the database you want to truncate in the databases-list (left-hand-side)
    • In the main view scroll down, you'll see a checkbox "Check All" and next to it a drop-down box where you can select "Empty". Done.
    0 讨论(0)
  • 2021-02-13 03:46

    Actually, went deeper and wrote this:

    SET @command:='';
    SELECT @command:=CONCAT(@command, 'TRUNCATE TABLE ',T.TABLE_NAME,';')
    FROM INFORMATION_SCHEMA.tables T
    WHERE T.table_type = 'BASE TABLE' AND T.table_schema='YOUR_TABLE_SCHEMA';
    PREPARE bye_world FROM @command;
    EXECUTE bye_world;
    DEALLOCATE PREPARE bye_world;
    

    It selects all table names from provided schema YOUR_TABLE_SCHEMA and puts them into a @command user variable, forming a query for each one like this: TRUNCATE TABLE TABLE_NAME;

    Then i just prepare selected statement and execute it. Note, that you must declare user variable before query, or it will be 0 and mess up our statement.

    Tutorial

    Using MySQL DROP TABLE To Remove Existing Tables

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

    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:48

    You can delete all data from phpmyadmin function easily. Maybe this feature didn't exists during 2010 when this question was posted, but I feel all beginners can refer to this.

    Delete all data from database

    0 讨论(0)
提交回复
热议问题