SQL查询数据库名、表名和表字段名

本文介绍了在Oracle、PostgreSQL和MySQL数据库中查询数据库名、表名以及表字段名和类型的SQL语句。在Oracle中,可以查询表空间但不直接支持数据库名查询;PostgreSQL提供了数据库名、表名及其备注的查询;而MySQL则使用information_schema来获取这些信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 Oracle

1,数据库名

-- Oracle没有提供数据库名称查询支持,只提供了表空间名称查询。
-- 查询表空间(需要一定权限)
SELECT
	* 
FROM
	v$tablespace;

2,表名

-- 查询当前数据库中所有表名
SELECT
	* 
FROM
	user_tables;

3,表字段名及类型

-- 查询指定表中的所有字段名
-- 表名要全大写
select column_name from user_tab_columns where table_name = 'table_name';    

-- 查询指定表中的所有字段名和字段类型
select column_name, data_type from user_tab_columns where table_name = 'table_name'; 

-- 字段注释
SELECT
	ut.column_name,--字段名称
	uc.comments,--字段注释
	ut.DATA_TYPE,--字典类型
	ut.DATA_LENGTH,--字典长度
	ut.NULLABLE,--是否为空
	ut.DATA_DEFAULT 
FROM
	user_tab_columns ut
	INNER JOIN user_col_comments uc ON ut.TABLE_NAME = uc.table_name 
	AND ut.COLUMN_NAME = uc.column_name 
WHERE
	ut.Table_Name = 'table_name' 
ORDER BY
	ut.column_name

二 postgresql

1,数据库名

SELECT oid, datname, datcollate, datctype FROM pg_database ORDER BY datname;

2,表名

查询全部表名和备注

select relname as tabname,cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c
where relname in (select tablename from pg_tables where schemaname='public' and position('_2' in tablename)=0);

查询指定表名及备注

select relname as tabname,cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c
where relname in (select tablename from pg_tables where schemaname='public' and position('_2' in tablename)=0)
and relname = 'fm_businxxxxx';

3,表字段名

查询指定表字段名,类型

select a.attnum,a.attname,concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) from '\(.*\)')) as type,d.description from pg_class c,pg_attribute a,pg_type t,pg_description d
where c.relname='fm_business' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oid and d.objoid=a.attrelid and d.objsubid=a.attnum;

三 MySQL

1,数据库名

show databases;

2,表名

查询全部表名和备注

SELECT
	TABLE_NAME,
	ENGINE,
	TABLE_SCHEMA 
FROM
	information_schema.TABLES 
WHERE
	table_schema = 'dm' -- 数据库名

查询指定表名

SELECT
	TABLE_NAME,
	ENGINE,
	TABLE_COLLATION,
	TABLE_SCHEMA
FROM
	information_schema.TABLES 
WHERE
	table_schema = 'dm' --数据库名
	AND TABLE_NAME LIKE '%cm_apply_relation%'; -- 表名

3,表字段名

查询指定表字段名,类型

SELECT
	COLUMN_NAME,
	DATA_TYPE,
	COLUMN_COMMENT,
	CHARACTER_SET_NAME,
	COLUMN_TYPE
FROM
	information_schema.COLUMNS 
WHERE
	table_name = 'cm_apply_relation'  -- 表名
	AND table_schema = 'dm'; -- 数据库名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值