Delete data from all tables in MYSQL

前端 未结 16 1683
自闭症患者
自闭症患者 2020-12-13 09:17

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

相关标签:
16条回答
  • 2020-12-13 09:59

    Begin by accessing phpMyAdmin via cPanel or Plesk.

    1. Select the database you wish to work with (from the list of databases on the left column when on the phpMyAdmin home page).
    2. A list of tables will appear in the left column and in the wider right column.
    3. Check the table(s) you wish to truncate (delete data only).
    4. In the drop down box that initially says “With selected:” select “Empty” from this list.
    5. It will ask you if you really want to truncate the table(s). Check to make sure you selected the tables you really want to truncate.
    6. Click on Yes to truncate (delete data) from the table.
    0 讨论(0)
  • 2020-12-13 10:01

    I don't think so (but I've been wrong before). What I tend to do is those cases is a two-step process.

    If your DBMS has a command line interface, you can use it to create a script to do the bulk of the work, something like:

    db2 "select 'db2 delete from ' | tblname from sysibm.systables
        where owner = 'pax'" >p2.sh
    p2.sh
    

    The first bit simply creates a p2.sh file (or a p2.cmd file under Windows) containing a delete from statement for every table owned by pax. Then you just run that command file to do the dirty work. You may want to check it first, of course :-)

    Not the one-step process you were looking for but still very simple. I'm assuming here that mysql also has a command line interface.

    Update:

    The MySQL version of the above looks like it should be:

    echo "select 'mysql truncate table ' | table_name
                  from information_schema.tables" | mysql >p2.sh
    bash p2.sh
    

    This uses the truncate command which is usually more efficient than delete from for deleting all rows. It also uses the proper MySQL system tables to get the table names.

    One point though - you may want to put a where clause on that select to limit the tables to those you want deleted. The query as it stands will try to delete every table. One possibility is to limit it with specific table_schema and/or table_type values.

    0 讨论(0)
  • 2020-12-13 10:03
    $ mysqldump --no-data -u [username] -p[password] [database] > [location]
    $ mysql -u [username] -p[password]
    mysql > drop database [database]; create database [database];
    mysql > exit;
    $ mysql -u [username] -p[password] [database] < [location]
    

    The --no-data switch preserves only the database table information, and will ignore all table data; you merely have to reimport the generated .sql file to regain all table information.

    0 讨论(0)
  • 2020-12-13 10:04

    It's possible, but it has side-effects you might not like:

    drop database <dbname>;
    

    It means the tables' structures are deleted, as well as the indexes, stored procedures, etc., etc.

    The only other way would be to write a stored procedure which loops somehow with

    truncate table <tablename>;
    
    0 讨论(0)
  • 2020-12-13 10:04

    Why couldnt you just export the structure of the database, delete the database, then recreate it and import the structure?

    0 讨论(0)
  • 2020-12-13 10:05

    Export the SQL script, delete the database, recreate the database against the script. :)

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