【数据库】四大传统数库据元数据查询

2023-11-01 05:12

本文主要是介绍【数据库】四大传统数库据元数据查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Mysql

-- 查询列信息
select t.table_catalog,t.table_schema,t.table_name,t.table_type,table_rows
,column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length,numeric_precision,numeric_scale,datetime_precision,column_type,character_set_name,collation_name
from `information_schema`.`tables` t 
inner join `information_schema`.`columns` c 
on t.table_catalog=c.table_catalog and t.table_schema=c.table_schema and t.table_name=c.table_name
where 1=1
and t.table_schema='test' 
and t.table_name='test_type_meta_basic';-- 查询索引信息
show index from test.test_type_meta_basic;-- 查询表的约束
select t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type ,group_concat(c.column_name order by ordinal_position) column_names
from `information_schema`.`table_constraints` t inner join `information_schema`.`key_column_usage` c on 1=1 
and t.table_schema ='test' 
and t.table_name='test_type_meta_basic' 
and t.table_schema =c.table_schema
and t.table_name=c.table_name
and t.constraint_name =c.constraint_name
group by t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type;

Postgres

-- 查询列信息
select t.table_catalog,t.table_schema,t.table_name,t.table_type
,column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length,numeric_precision,numeric_scale,datetime_precision,udt_name
from "information_schema"."tables" t 
inner join "information_schema"."columns" c 
on t.table_catalog=c.table_catalog and t.table_schema=c.table_schema and t.table_name=c.table_name
where t.table_schema='ischema' and t.table_name='test_type_meta_basic';-- 查询索引列
select t.tablename,t.indexname,t.indexdef from "pg_catalog"."pg_indexes" t 
where t.schemaname='ischema' and t.tablename ='test_type_meta_basic' -- 查询表的约束
select t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type ,string_agg(c.column_name,',' order by ordinal_position) column_names
from "information_schema"."table_constraints" t inner join "information_schema"."key_column_usage" c on 1=1 
and t.table_schema ='ischema' 
and t.table_name='test_type_meta_basic' 
and t.table_schema =c.table_schema
and t.table_name=c.table_name
and t.constraint_name =c.constraint_name
group by t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type;

Sqlserver

-- 查询列信息
select t.table_catalog,t.table_schema,t.table_name,t.table_type
,column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length,numeric_precision,numeric_scale,datetime_precision,collation_name
from "INFORMATION_SCHEMA"."TABLES" t 
inner join "INFORMATION_SCHEMA"."COLUMNS" c 
on t.table_catalog=c.table_catalog and t.table_schema=c.table_schema and t.table_name=c.table_name
where t.table_schema='ischema' and t.table_name='test_type_meta_basic';-- 查询索引信息
select table_schema,table_name,index_name,index_type,index_type_desc,is_unique,is_primary_key
,string_agg(column_name,',' ) WITHIN GROUP ( ORDER BY key_ordinal ASC)  column_names from (
select s.name table_schema,t.name table_name,c.name column_name
,i.name index_name,i.type index_type,i.type_desc index_type_desc,i.is_unique,i.is_primary_key
,ic.key_ordinal
from sys.schemas s 
inner join "sys"."tables" t on s.schema_id =t.schema_id 
inner join "sys"."indexes" i on t.object_id =i.object_id 
inner join "sys"."index_columns" ic on i.object_id =ic.object_id and i.index_id=ic.index_id
inner join "sys"."columns" c on ic.object_id =c.object_id  and ic.column_id = c.column_id 
where t.name ='test_type_meta_basic'
) tmp group by table_schema,table_name,index_name,index_type,index_type_desc,is_unique,is_primary_key;-- 查询表的约束
select t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type ,string_agg(c.column_name,',') within group (order by ordinal_position) column_names
from "INFORMATION_SCHEMA"."TABLE_CONSTRAINTS" t inner join "INFORMATION_SCHEMA"."KEY_COLUMN_USAGE" c on 1=1 
and t.table_schema ='ischema' 
and t.table_name='test_type_meta_basic' 
and t.table_schema =c.table_schema
and t.table_name=c.table_name
and t.constraint_name =c.constraint_name
group by t.constraint_schema,t.table_name,t.constraint_name,t.constraint_type;

Oracle

-- 查询列信息
select t.OWNER ,t.TABLE_NAME
,COLUMN_NAME,COLUMN_ID,NULLABLE,DATA_TYPE,DATA_PRECISION,DATA_SCALE,CHARACTER_SET_NAME
from "SYS"."ALL_TABLES" t 
inner join "SYS"."ALL_TAB_COLS" c 
ON t.OWNER=c.OWNER and t.TABLE_NAME=c.TABLE_NAME
where t.OWNER ='CDCUSER' and t.TABLE_NAME='TEST_TYPE_META_BASIC' ORDER BY COLUMN_ID;-- 查询索引信息
SELECT TABLE_NAME,INDEX_NAME,LISTAGG(COLUMN_NAME,',') WITHIN GROUP (ORDER BY COLUMN_POSITION) agg 
FROM "ALL_IND_COLUMNS" 
WHERE TABLE_NAME='TEST_TYPE_META_BASIC' AND TABLE_OWNER ='CDCUSER'
GROUP BY TABLE_NAME,INDEX_NAME;-- 查询表的约束
SELECT t.TABLE_NAME ,t.CONSTRAINT_NAME,t.CONSTRAINT_TYPE ,LISTAGG(c.COLUMN_NAME,',') WITHIN GROUP (ORDER BY POSITION) COLUMN_NAMES
FROM ALL_CONSTRAINTS t,ALL_CONS_COLUMNS c 
WHERE t.TABLE_NAME =c.TABLE_NAME 
AND t.OWNER=c.OWNER AND t.CONSTRAINT_NAME=c.CONSTRAINT_NAME 
AND t.OWNER='CDCUSER' AND t.TABLE_NAME='TEST_TYPE_META_BASIC'
GROUP BY t.TABLE_NAME ,t.CONSTRAINT_NAME,t.CONSTRAINT_TYPE;

这篇关于【数据库】四大传统数库据元数据查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/320722

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE