常用数据库获取表,视图,列,索引信息

一、分页获取数据库用户的所有表

(1)、Oracle,OceanBase(Oracle内核版),DM

使用ALL_TABLES,需要添加当前用户作为查询条件

sql 复制代码
select a3.* from (select a2.* from  (select  a1.*, rownum rn1 from (
select t1.table_name, t2.comments from ALL_TABLES t1, ALL_TAB_COMMENTS t2 WHERE t1.table_name = t2.table_name and upper(t1.owner) = upper(?)
) a1) a2 where rn1 <= 10 ) a3 where rn1 >= 1;

使用USER_TABLES,直接就可以查到当前用户下的表

sql 复制代码
select a3.* from (select a2.* from  (select  a1.*, rownum rn1 from (
select t1.table_name, t2.comments from USER_TABLES t1, USER_TAB_COMMENTS t2 WHERE t1.table_name = t2.table_name
) a1) a2 where rn1 <= 10 ) a3 where rn1 >= 1;

(2)、MySQL

sql 复制代码
select t.* from (select table_name,table_type,TABLE_COMMENT from INFORMATION_SCHEMA.TABLES where upper(table_schema) = upper(?) and table_type = 'BASE TABLE') t limit 1,10;

(3)、gaussdb

sql 复制代码
select t.* from (select table_name,table_type from INFORMATION_SCHEMA.TABLES where upper(table_catalog) = upper(?) and table_type = 'BASE TABLE') t limit 10 offset 1;

(4)、PostgreSQL

sql 复制代码
select t.* from (select table_name,table_type from INFORMATION_SCHEMA.TABLES where upper(table_schema) = upper(?) and table_type = 'BASE TABLE') t limit 10 offset 1;

二、分页获取数据库用户的所有视图

(1)、Oracle,OceanBase(Oracle内核版),DM

使用ALL_TABLES,需要添加当前用户作为查询条件

sql 复制代码
select a3.* from (select a2.* from  (select  a1.*, rownum rn1 from (
select t1.view_name, t2.comments from ALL_VIEWS t1, ALL_TAB_COMMENTS t2 WHERE t1.view_name = t2.table_name and upper(t1.owner) = upper(?)
) a1) a2 where rn1 <= 10 ) a3 where rn1 >= 1;

使用USER_TABLES,直接就可以查到当前用户下的表

sql 复制代码
select a3.* from (select a2.* from  (select  a1.*, rownum rn1 from (
select t1.view_name, t2.comments from USER_VIEWS t1, USER_TAB_COMMENTS t2 WHERE t1.view_name = t2.table_name
) a1) a2 where rn1 <= 10 ) a3 where rn1 >= 1;

(2)、MySQL

sql 复制代码
select t.* from (select table_name,table_type,TABLE_COMMENT from INFORMATION_SCHEMA.TABLES where upper(table_schema) = upper(?) and table_type = 'VIEW') t limit 1,10;

(3)、gaussdb

sql 复制代码
select t.* from (select table_name,table_type from INFORMATION_SCHEMA.TABLES where upper(table_catalog) = upper(?) and table_type = 'VIEW') t limit 10 offset 1;

(4)、PostgreSQL

sql 复制代码
select t.* from (select table_name,table_type from INFORMATION_SCHEMA.TABLES where upper(table_schema) = upper(?) and table_type = 'VIEW') t limit 10 offset 1;

三、查询数据库用户表的列信息

(1)、Oracle,OceanBase(Oracle内核版),DM

使用ALL_TAB_COLUMNS,需要添加当前用户作为查询条件

sql 复制代码
select * from ALL_TAB_COLUMNS where upper(table_name) = upper(?) and upper(owner) = upper(?);

使用USER_TAB_COLUMNS,直接就可以查到当前用户下的表

sql 复制代码
select * from USER_TAB_COLUMNS where upper(table_name) = upper(?);

(2)、MySQL,gaussdb

sql 复制代码
select * from information_schema.COLUMNS where upper(table_name) = upper(?) and upper(table_schema) = upper(?);

(3)、PostgreSQL

sql 复制代码
select * from information_schema.COLUMNS where upper(table_name) = upper(?) and upper(table_catalog) = upper(?);

四、获取数据库用户的所有表索引

(1)、Oracle,OceanBase(Oracle内核版),DM

使用ALL_IND_COLUMNS,需要添加当前用户作为查询条件

sql 复制代码
select * from ALL_IND_COLUMNS where upper(table_name) = upper(?) AND upper(index_owner) = upper(?);

使用USER_IND_COLUMNS,直接就可以查到当前用户下的表

sql 复制代码
select * from USER_IND_COLUMNS where upper(table_name) = upper(?);

(2)、MySQL

sql 复制代码
select * from INFORMATION_SCHEMA.STATISTICS where upper(table_name) = upper(?) and upper(table_schema) = upper(?);

(3)、gaussdb,PostgreSQL

sql 复制代码
select * from pg_indexes t where upper(tablename) = upper(?) and upper(schemaname) = upper(?);
相关推荐
落笔画忧愁e13 分钟前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
Σίσυφος190027 分钟前
halcon 条形码、二维码识别、opencv识别
前端·数据库
小刘|1 小时前
深入理解 SQL 注入漏洞及解决方案
数据库·sql
天上掉下来个程小白2 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
哆木2 小时前
排查生产sql查询缓慢
数据库·sql·mysql
橘子师兄3 小时前
分页功能组件开发
数据库·python·django
book01213 小时前
MySql数据库运维学习笔记
运维·数据库·mysql
纠结哥_Shrek3 小时前
Oracle和Mysql的区别
数据库·mysql·oracle
极客先躯3 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
做梦敲代码3 小时前
达梦统计信息
数据库·达梦数据库