search

Home  >  Q&A  >  body text

mysql - 删除数据库中的数据,但是表结构保留

mysql如何捉到,删除数据库中的数据,但是表结构保留
每个表都清空数据,但是各个表的字段都保留。
请问有20个表的话,就必须把说有的表名引用一遍?
写20遍?
delete from tablename1;
delete from tablename20;

伊谢尔伦伊谢尔伦2787 days ago672

reply all(7)I'll reply

  • 阿神

    阿神2017-04-17 15:33:58

    <?php
    //用循环执行SQL,瞬间帮帮你删除干净了,即使是100张表 也是瞬间
    for($i=0;$i<=20;$i++)
    {
        $sql = 'DELETE FROM tableName'.$i;
        $db->query($sql);
    }
    
    
    
    
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:33:58

    Run this statement and it will generate all table deletion statements for the specified database. Just take the result query and be sure to replace your database name

    SELECT Group_concat(a.sql_truncate, a.sql_truncate, '') AS sql_truncate 
    FROM   (SELECT Concat('truncate table ', table_name, ';') AS sql_truncate 
            FROM   information_schema.tables 
            WHERE  table_schema = '你的数据库名' 
                   AND table_type = 'BASE TABLE') AS a 
    

    truncate table table_name

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:33:58

    mysql -uroot test -e "show tables" > all_table.md
    然后用vim或者sublime编辑all_table.md在前面统一加上delete from 

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:33:58

    Write stored procedure
    Read table name and execute delete in loop
    delete from tablename;

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:33:58

    Export the table structure and delete the database. Rebuild

    reply
    0
  • 阿神

    阿神2017-04-17 15:33:58

    drop user username cascade
    Cascade delete all data under the user

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:33:58

    Don’t use delete, use truncate

    reply
    0
  • Cancelreply