【数据库-MySql】清空所有表格的所有数据
2017-12-22 15:06
459 查看
方式一、
drop procedure if exists del_all_tb;
delimiter $$
create procedure del_all_tb(db char(20))
begin
declare done int default 0;
declare tb char(100);
declare cur cursor for select table_name from infoRmation_schema.tables where table_schema = db and table_type = "BASE TABLE";
declare continue handler for not found set done = 1;
open cur;
repeat
fetch cur into tb;
set @sql := concat("truncate ", tb, ";");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
until done end repeat;
close cur;
end $$
delimiter ;
call del_all_tb("atdps");
drop procedure if exists del_all_tb;
方式二、
#如果存在del_all_tb存储过程则删除del_all_tb存储过程
drop procedure if exists del_all_tb;
#如果存在 tmpTable 临时表则删除 del_all_tb 临时表
DROP TABLE if EXISTS tmpTable;
#创建 del_all_tb存储过程
create procedure del_all_tb(db char(20))
begin
#申明变量
DECLARE tntmp VARCHAR(100);
#创建临时表
create table tmpTable (tablename VARCHAR(100),flag int);
#清空临时表
truncate TABLE tmpTable;
#将需要清空的表插入到临时表
INSERT INTO tmpTable(tablename , flag ) (SELECT table_name ,0 as a FROM information_schema.tables
WHERE table_schema = db and table_type='BASE TABLE');
#循环获取所有的表明以及删除状态
SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
WHILE tntmp <> '' DO
#拼写删除语句
set @sqlText := concat("truncate ", tntmp, ";");
prepare stmt from @sqlText;
#执行语句
execute stmt;
#释放删除语句
deallocate prepare stmt;
#更新表状态
UPDATE tmpTable SET flag=1 WHERE tablename = tntmp;
#选择一下条语句
SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
END WHILE;
end;
call del_all_tb("atdps");
#如果存在del_all_tb存储过程则删除del_all_tb存储过程
drop procedure if exists del_all_tb;
#如果存在 tmpTable 临时表则删除 del_all_tb 临时表
DROP TABLE if EXISTS tmpTable;
drop procedure if exists del_all_tb;
delimiter $$
create procedure del_all_tb(db char(20))
begin
declare done int default 0;
declare tb char(100);
declare cur cursor for select table_name from infoRmation_schema.tables where table_schema = db and table_type = "BASE TABLE";
declare continue handler for not found set done = 1;
open cur;
repeat
fetch cur into tb;
set @sql := concat("truncate ", tb, ";");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
until done end repeat;
close cur;
end $$
delimiter ;
call del_all_tb("atdps");
drop procedure if exists del_all_tb;
方式二、
#如果存在del_all_tb存储过程则删除del_all_tb存储过程
drop procedure if exists del_all_tb;
#如果存在 tmpTable 临时表则删除 del_all_tb 临时表
DROP TABLE if EXISTS tmpTable;
#创建 del_all_tb存储过程
create procedure del_all_tb(db char(20))
begin
#申明变量
DECLARE tntmp VARCHAR(100);
#创建临时表
create table tmpTable (tablename VARCHAR(100),flag int);
#清空临时表
truncate TABLE tmpTable;
#将需要清空的表插入到临时表
INSERT INTO tmpTable(tablename , flag ) (SELECT table_name ,0 as a FROM information_schema.tables
WHERE table_schema = db and table_type='BASE TABLE');
#循环获取所有的表明以及删除状态
SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
WHILE tntmp <> '' DO
#拼写删除语句
set @sqlText := concat("truncate ", tntmp, ";");
prepare stmt from @sqlText;
#执行语句
execute stmt;
#释放删除语句
deallocate prepare stmt;
#更新表状态
UPDATE tmpTable SET flag=1 WHERE tablename = tntmp;
#选择一下条语句
SELECT tablename into tntmp FROM tmpTable WHERE flag = 0 limit 1;
END WHILE;
end;
call del_all_tb("atdps");
#如果存在del_all_tb存储过程则删除del_all_tb存储过程
drop procedure if exists del_all_tb;
#如果存在 tmpTable 临时表则删除 del_all_tb 临时表
DROP TABLE if EXISTS tmpTable;
相关文章推荐
- Mysql如何清空数据库的所有表数据
- 清空数据库中所有表数据的方法
- 清空数据库中所有表数据的方法
- MySQL入门--数据表的创建、显示指定数据库中的所有数据表
- 使用SQL语句清空数据库所有表的数据(转)
- 【MYSQL 清空所有的的表中的数据的SQL的生成】
- 用mysql扩展类把数据库数据取出放在表格上
- 清空一个数据库中所有数据
- SQL Server 如何清空数据库所有表数据
- SQL Server 自增字段重置与清空数据库中所有表数据的方法
- 如何在jsp上查询并显示数据库mysql的数据表格
- 清空数据库所有表数据
- MySQL入门--数据表的创建、显示指定数据库中的所有数据表
- jQuery实现清空table表格除首行外的所有数据
- Mysql导出数据库的表和所有的数据指令
- SQL 清空数据库的所有表数据
- mysql 怎样清空一个数据库中的所有表
- 清空数据库中所有表数据的方法(慎用)
- SQL Server 清空或删除所有数据库表中的数据
- jqGrid清空表格中的所有行数据