开发者

Clean everything from database tables

I have some database with a lot tables , and i want to clean the data from tables how i can do it ?

like doing for l开发者_如何学JAVAoop on every table in the database DELETE FROM table_name;

Thanks.


Another alternative

  1. use mysqldump to dump table schema only
  2. use add-drop table
  3. once you have the dump file, execute it
  4. all the table will be drop and re-create

If you want to reset your auto increment,
then make changes to the dump file to reset it


if you are using linux, this can be done by a bash script, like

for table in $(mysql -N <<<"show tables from your_db")
do
  mysql -N <<< "truncated $table"
done


One way is to create a script which will generate all the sql statements for you. Here is a version for linux:

echo "select concat('truncate ', table_name, ';') as '' from \
  information_schema.tables where table_schema='YOURDATABASE';"\
  |mysql -u USER -p > /tmp/truncate-all-tables.sql


The simplest way to do this is simply to issue a DELETE or TRUNCATE command on a per-table basis either from a .sql batch file or via a scripting language. (Whilst it's possible to interrogate the table schema information and use this, this may not be suitable depending on whether or not you have the required access privs and whether the tables in question follow a SELECTable naming convention.)

Incidentally, whilst you can use DELETE, this won't reclaim the used disk space and any auto_increment fields will remember the previous ID. (If you want to get the disk space back and reset the IDs to 1, use "TRUNCATE <table name>;".)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜