Can I Truncate All Tables in a MySQL Database with a Single Command?
Dec 10, 2024 pm 07:27 PMTruncating All Tables in a MySQL Database with a Single Command
The question arises: is it possible to truncate all tables in a MySQL database in a single operation?
Absolutely! Here's a solution that combines both drop and truncate commands to achieve your objective:
Drop Tables
The following command drops (i.e., removes) all tables in a database:
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done
Truncate Tables
Alternatively, you can use a truncate command to empty all tables instead of dropping them:
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "truncate table $table" DATABASE_NAME; done
These commands will effectively purge all tables from the specified database in one swift command.
The above is the detailed content of Can I Truncate All Tables in a MySQL Database with a Single Command?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library

Run MySQl in Linux (with/without podman container with phpmyadmin)

What is SQLite? Comprehensive overview

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections?
