数据库初始化脚本(用 truncate 命令一键清空某个数据库中全部数据表数据)

数据库初始化脚本(用 truncate 命令一键清空某个数据库中全部数据表数据)

在开发中,当数据表结构有变动或者数据库中有脏数据时,想要清空数据表中的数据重新来过,数据表一张接着一张地清空数据比较麻烦,现在只需两步便可以清空数据库中全部数据表数据。

1.执行下面的sql语句生成"清空数据库的sql脚本"

bash 复制代码
select GROUP_CONCAT(CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';')separator '')
from INFORMATION_SCHEMA.TABLES where table_schema in ('数据库名') and table_name like
'%模糊表名%'

例如我想清空earthwire数据库下的全部数据表,可以执行以下语句生成一个sql脚本

生成的能够"清空数据库的sql脚本"如下:

bash 复制代码
truncate TABLE earthwire.t_camera;
truncate TABLE earthwire.t_earthwire;
truncate TABLE earthwire.t_earthwire_device;
truncate TABLE earthwire.t_earthwire_record;
truncate TABLE earthwire.t_history_task;
truncate TABLE earthwire.t_picture;
truncate TABLE earthwire.t_system_configuration;
truncate TABLE earthwire.t_task;
truncate TABLE earthwire.t_task_device;

2.执行"清空数据库的sql脚本"

执行完毕后,刷新数据库就会发现数据库空空如也啦

相关推荐
不羁。。1 小时前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
yangchanghua1113 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance3 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai3 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
在努力的前端小白3 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务3 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
冒泡的肥皂6 小时前
MVCC初学demo(一
数据库·后端·mysql
.Shu.7 小时前
Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
数据库·redis·架构
Bruce_Liuxiaowei9 小时前
MySQL完整重置密码流程(针对 macOS)
mysql
麦麦大数据10 小时前
F003疫情传染病数据可视化vue+flask+mysql
mysql·flask·vue·大屏·传染病