SQL语句查询所有表和查询表里的所有字段

Posted mp-ui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句查询所有表和查询表里的所有字段相关的知识,希望对你有一定的参考价值。

查询数据库里的所有表

1. 查询数据库里面的所有表

mysql

select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
			where table_schema = (select database()) and table_name = #tableName

SqlServer

select * from (
			select cast(so.name as nvarchar(500)) as tableName, 'mssql' as engine,cast(sep.value as nvarchar(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 t.tableName=#tableName

Oracle

select dt.table_name tableName,dtc.comments tableComment,dt.last_analyzed createTime from user_tables dt,user_tab_comments dtc where dt.table_name=dtc.table_name and dt.table_name = UPPER(#tableName)

2. 查询数据表里的所有字段

MySQL

select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
 			where table_name = #tableName and table_schema = (select database()) order by ordinal_position

SQLServer

SELECT
		cast(
			b.NAME AS NVARCHAR(500)
		) AS columnName,
		cast(
			sys.types.NAME AS NVARCHAR(500)
		) AS dataType,
		cast(
			c.VALUE AS NVARCHAR(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'

Oracle

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

3. 所有表及其字段一起查询

MySQL

SELECT
    t.table_name,
    GROUP_CONCAT(c.column_name,'') AS columns
FROM information_schema.tables t
         LEFT JOIN information_schema.columns c on c.table_name = t.table_name
WHERE t.table_schema = (SELECT database()) GROUP BY t.table_name

SqlServer

SELECT
       SUBSTRING(t.table_name, LENGTH('$prefix_') + 1) AS tableName,
       GROUP_CONCAT((CASE data_type WHEN 'datetime' THEN CONCAT('CONVERT(varchar(100),', column_name, ',20) AS ', column_name) ELSE column_name END),'') AS columns
FROM information_schema.tables t
LEFT JOIN information_schema.columns c on c.table_name = t.table_name
WHERE t.table_schema = (SELECT database())
  AND t.table_name LIKE '$prefix|_%' escape '|'
GROUP BY t.table_name

以上是关于SQL语句查询所有表和查询表里的所有字段的主要内容,如果未能解决你的问题,请参考以下文章

怎样用SQL查询一个表的所有字段

ORACLE怎么根据字段值找出所有的表和字段?

SQL如何查询一张表的所有字段并按其中一个字段进行分组

用一条sql语句查询表中字段的所有首字母大写

在pl/sql中怎么查询所有存在的表,以及怎么样获得未知表中的某一字段

sql查询大于字段的所有数据,或小于字段的所有数据