简体   繁体   中英

How to get all tables and column names in SQL?

I want a list of table names and columns within that table (see below). Is there a SQL query that would allow me to do this within a schema?
结果我想要
I know I can look at the GUI interface to look at the table names and columns but there are too many to look at manually.

但是你的问题还不够明确,但你可以用这个代码得到所有这些

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

Using OBJECT CATALOG VIEWS:

SELECT T.name AS Table_Name ,
   C.name AS Column_Name ,
   P.name AS Data_Type ,
   P.max_length AS Size ,
   CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale
FROM   sys.objects AS T
   JOIN sys.columns AS C ON T.object_id = C.object_id
   JOIN sys.types AS P ON C.system_type_id = P.system_type_id
WHERE  T.type_desc = 'USER_TABLE';

Using INFORMATION SCHEMA VIEWS

SELECT TABLE_SCHEMA ,
   TABLE_NAME ,
   COLUMN_NAME ,
   ORDINAL_POSITION ,
   COLUMN_DEFAULT ,
   DATA_TYPE ,
   CHARACTER_MAXIMUM_LENGTH ,
   NUMERIC_PRECISION ,
   NUMERIC_PRECISION_RADIX ,
   NUMERIC_SCALE ,
   DATETIME_PRECISION
FROM   INFORMATION_SCHEMA.COLUMNS;

Taken from this answer: Getting list of tables, and fields in each, in a database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM