数据库查询所有表及实现拼接清表sql

场景:

实施想让我提供一下清除库中所有表的数据脚本,我只知道一个show tables查询所有表

本想着select concat('delete from ',show tables)发现执行不了,总结不同数据库清表拼接语句。

mysql

查询所有表sql

show tables

拼接成清空库中所有表sql

select GROUP_CONCAT(concat('delete from ',table_name) SEPARATOR ";") delsql from information_schema.`TABLES` where table_schema='表约束名称';

PostgreSQL 

查询所有表

方式一:通过pg_tables系统目录表查询

select tablename from pg_tables where schemaname='表约束名称';

方式二:通过information_schema.tables视图查询

select table_name  FROM information_schema.tables where table_schema='表约束名称' ;

拼接成清空库中所有表sql

SELECT 'TRUNCATE ' || string_agg(format('%I.%I', table_schema, table_name), ', ') || ' CASCADE;'
FROM information_schema.tables
WHERE table_schema = '表约束名' and table_catalog='系统类目名'; 

扫描二维码关注公众号,回复: 15534312 查看本文章

ORACLE

查询所有表

方式一:查询当前用户中的表名

select t.table_name from user_tables t where t.TABLESPACE_NAME='表创建用户名';

方式二:查询所有用户中的表名 

select t.table_name from all_tables t where t.TABLESPACE_NAME='表创建用户名';

拼接成清空库中所有表sql 

 SELECT 'DELETE FROM ' || table_name || ';' FROM user_tables ;

猜你喜欢

转载自blog.csdn.net/qq_38423256/article/details/130486551