數據庫中查詢表的sql及表的字段信息

mysqlnode

查詢數據中的表mysql

select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tablessql

where table_schema = (select database())sqlserver

<if test="tableName != null and tableName.trim() != ''">orm

and table_name like concat('%', #{tableName}, '%')server

</if>ip

查詢表的字段信息get

select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columnsit

where table_name = #{tableName} and table_schema = (select database()) order by ordinal_positionio

Oracle

查詢數據中的表 select dt.table_name tableName,

dtc.comments tableComment,

uo.created createTime

from user_tables dt,

user_tab_comments dtc,

user_objects uo

where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type='TABLE'

<if test="tableName != null and tableName.trim() != ''">

and dt.table_name like concat('%', UPPER(#{tableName}))

</if>

查詢表的字段信息

select temp.column_name columnname,

temp.data_type dataType,

temp.comments columnComment,

case temp.constraint_type when 'P' then 'PRI' when 'C' then 'UNI' else '' end "COLUMNKEY",

'' "EXTRA"

from (

select col.column_id,

col.column_name,

col.data_type,

colc.comments,

uc.constraint_type,

-- 去重

row_number() over (partition by col.column_name order by uc.constraint_type desc) as row_flg

from user_tab_columns col

left join user_col_comments colc

on colc.table_name = col.table_name

and colc.column_name = col.column_name

left join user_cons_columns ucc

on ucc.table_name = col.table_name

and ucc.column_name = col.column_name

left join user_constraints uc

on uc.constraint_name = ucc.constraint_name

where col.table_name = upper(#{tableName})

) temp

where temp.row_flg = 1

order by temp.column_id

PostgreSQL

查詢數據中的表 select t1.tablename as tableName, obj_description(relfilenode, 'pg_class') as tableComment, now() as createTime from pg_tables t1, pg_class t2

where t1.tablename not like 'pg%' and t1.tablename not like 'sql_%' and t1.tablename = t2.relname

<if test="tableName != null and tableName.trim() != ''">

and t1.tablename like concat('%', #{tableName}, '%')

</if>

查詢表的字段信息

select t2.attname as columnName, pg_type.typname as dataType, col_description(t2.attrelid,t2.attnum) as columnComment, '' as extra,

(CASE t3.contype WHEN 'p' THEN 'PRI' ELSE '' END) as columnKey

from pg_class as t1, pg_attribute as t2 inner join pg_type on pg_type.oid = t2.atttypid

left join pg_constraint t3 on t2.attnum = t3.conkey[1] and t2.attrelid = t3.conrelid

where t1.relname = #{tableName} and t2.attrelid = t1.oid and t2.attnum>0

sqlserver

查詢數據中的表

select * from(

select cast(so.name as varchar(500)) as tableName, cast(sep.value as varchar(500)) as tableComment, getDate() as createTime

from sysobjects so

left JOIN sys.extended_properties sep

on sep.major_id=so.id and sep.minor_id=0

where (xtype='U' or xtype='v')

) t where 1=1

<if test="tableName != null and tableName.trim() != ''">

and t.tableName like concat('%', #{tableName}, '%')

</if>

查詢表的字段信息

SELECT

cast(

b.NAME AS VARCHAR(500)

) AS columnName,

cast(

sys.types.NAME AS VARCHAR(500)

) AS dataType,

cast(

c.VALUE AS VARCHAR(500)

) AS columnComment,

(

SELECT

CASE

count( 1 )

WHEN 1 then 'PRI'

ELSE ''

END

FROM

syscolumns,

sysobjects,

sysindexes,

sysindexkeys,

systypes

WHERE

syscolumns.xusertype = systypes.xusertype

AND syscolumns.id = object_id(A.NAME)

AND sysobjects.xtype = 'PK'

AND sysobjects.parent_obj = syscolumns.id

AND sysindexes.id = syscolumns.id

AND sysobjects.NAME = sysindexes.NAME

AND sysindexkeys.id = syscolumns.id

AND sysindexkeys.indid = sysindexes.indid

AND syscolumns.colid = sysindexkeys.colid

AND syscolumns.NAME = B.NAME

) as columnKey,

'' as extra

FROM

(

select

name,

object_id

from

sys.tables

UNION all select

name,

object_id

from

sys.views

) a

INNER JOIN sys.COLUMNS b ON

b.object_id = a.object_id

LEFT JOIN sys.types ON

b.user_type_id = sys.types.user_type_id

LEFT JOIN sys.extended_properties c ON

c.major_id = b.object_id

AND c.minor_id = b.column_id

WHERE

a.NAME = #{tableName}

and sys.types.NAME != 'sysname'

相關文章
相關標籤/搜索