Delete the data of all tables in the Mysql database at one time, keeping the table structure

  The common method to delete data in a database table is to delete or truncate through the method of delete or truncate. If you delete a piece or part of the data in the table, it is suitable to use the delete operation to delete. If you want to delete all the data in the table, it is suitable It is the same as truncate for deletion.

        So the question is, if there are many tables in a certain database, and I want to delete all the data in all the tables in the database, what should I do?

        There are two main cases to solve this problem. If you do not need to keep the structure of all the tables in the database, then the answer is simple, execute the command drop database database name to achieve the goal. But if you need to keep the structure of all the tables in the database, and just want to delete the data in all the tables, how can you solve it? Some people will say that you can execute truncate a few more times and it’s OK. Yes, you can do it a few times more truncate. However, if there are many tables in the database to be deleted, dozens or hundreds of tables, execute it. Hundreds of truncate operations are obviously not a good way? So far, this is the method that this article will talk about.

        The way to delete is actually to execute the truncat method, but there is no need to manually enter the truncate command every time to delete. Generate all truncate statements through the sql command and write them into the .sql script file, and then execute the script to complete the delete operation and retain the table structure.

        The sql statement that generates the truncate command is: SELECT CONCAT('TRUNCATE TABLE',TABLE_NAME,';') FROM information_schema.TABLES WHERE TABLE_SCHEMA='test' into outfile'/tmp/truncate_test.sql';

        Then copy the generated .sql script to the current folder: mv /tmp/truncate_test.sql $current_dir/

        Then execute the .sql script to delete the data in all tables in the database: source $current_dir/truncate_test.sql

        Description:

        In the select...into outfile... operation, by default, only the file can be written to the tmp path, and you don’t need to move the .sql script under the tmp folder to the current folder, directly in Execute the .sql script under the tmp path. If you want to write to other paths, you need to grant write permissions to the mysql daemon. The easiest way is to modify SELINUX in the config configuration file under /etc/selinux to disabled to write to other paths. Next, the explanation is not expanded here.

        Combined with the previous blog post on mysql database backup and recovery, the source code and running screenshot of a small script are posted below. When using this script to back up and restore data, the structure of all tables in the source and destination databases must be consistent, otherwise the execution may go wrong. This script mainly implements the backup and recovery of the mysql database, and deletes all table data in the entire database at one time and retains the data table structure.

        Screenshot of running result:

        

        1): Back up all data in the test database

        2): Restore data

        3): Delete all data in all tables in the database test, and retain the table structure

        

        Script source code:

 

#/bin/bash
while [ 1 ]
do
    echo "  #############################################" 
    echo "           Quick Backup and Recovery"                   
    echo "    Source and Destination Mysql Must Be Same"
    echo "  #############################################"
    echo "    1) Back up database test"
    echo "    2) Recovery database test"
    echo "    3) Clear data in database test"
    echo "    q) Quit"
    echo -n " Your Option:"
    read option_char

    current_dir=$(pwd)
    case ${option_char} in
        "1")
    	    rm -f $current_dir/test_bk.sql
            mysqldump -t -c -uroot -proot test > $current_dir/test_backup.sql
            echo " Database test already backup..."
            ;;
        "2")
            mysql -u root --password='root' -e "
	    use test
            source $current_dir/test_backup.sql"
            echo " Database test already recovery..."
            ;;
        "3")
            rm -f $current_dir/truncate_test.sql
	    chmod 777 $current_dir
            mysql -u root --password='root' -e "
            SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM information_schema.TABLES WHERE TABLE_SCHEMA='test' into outfile '/tmp/truncate_test.sql';"
	    mv /tmp/truncate_test.sql $current_dir/
    	    mysql -u root --password='root' -e "
	    use test
	    source $current_dir/truncate_test.sql"
            echo " Clear data of test successful..."
            ;;
        "q"|"quit"|"exit"|'Q'|"QUIT"|"Quit")
	    break
	    ;;
        *)
            echo "your option is invalid, please input again..."
            ;;
    esac
done

Guess you like

Origin blog.csdn.net/u010460625/article/details/109049866