How do I list all the columns in a table?

前端 未结 12 676
挽巷
挽巷 2020-12-07 07:31

For the various popular database systems, how do you list all the columns in a table?

相关标签:
12条回答
  • 2020-12-07 08:01

    (5 years laters, for the Honor of PostgreSQL, the most advanced DDBB of the Kingdom)

    In PostgreSQL:

    \d table_name
    

    Or, using SQL:

    select column_name, data_type, character_maximum_length
        from INFORMATION_SCHEMA.COLUMNS 
        where table_name = 'table_name';
    
    0 讨论(0)
  • 2020-12-07 08:04

    For MS SQL Server:

    select * from information_schema.columns where table_name = 'tableName'
    
    0 讨论(0)
  • 2020-12-07 08:04

    SQL Server

    To list all the user defined tables of a database:

    use [databasename]
    select name from sysobjects where type = 'u'
    

    To list all the columns of a table:

    use [databasename]
    select name from syscolumns where id=object_id('tablename')
    
    0 讨论(0)
  • 2020-12-07 08:05

    Example:

    select Table_name as [Table] , column_name as [Column] , Table_catalog as [Database], table_schema as [Schema]  from information_schema.columns
    where table_schema = 'dbo'
    order by Table_name,COLUMN_NAME
    

    Just my code

    0 讨论(0)
  • Microsoft SQL Server Management Studio 2008 R2:

    In a query editor, if you highlight the text of table name (ex dbo.MyTable) and hit ALT+F1, you'll get a list of column names, type, length, etc.

    ALT+F1 while you've highlighted dbo.MyTable is the equivalent of running EXEC sp_help 'dbo.MyTable' according to this site

    I can't get the variations on querying INFORMATION_SCHEMA.COLUMNS to work, so I use this instead.

    0 讨论(0)
  • 2020-12-07 08:16

    Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):

    SELECT name
      FROM sys.columns 
      WHERE [object_id] = OBJECT_ID('dbo.tablename');
    
    0 讨论(0)
提交回复
热议问题