对于各种流行的数据库系统,如何列出表中的所有列?


当前回答

SQL Server

SELECT 
    c.name 
FROM
    sys.objects o
INNER JOIN
    sys.columns c
ON
    c.object_id = o.object_id
AND o.name = 'Table_Name'

or

SELECT 
    COLUMN_NAME 
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME  = 'Table_Name'

第二种方法是ANSI标准,因此应该适用于所有符合ANSI标准的数据库。

其他回答

对于MySQL,使用:

DESCRIBE name_of_table;

这也适用于Oracle,只要你使用SQL*Plus,或Oracle的SQL Developer。

SQL Server

使用实例列出数据库中所有用户定义的表。

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

列出一个表的所有列:

use [databasename]
select name from syscolumns where id=object_id('tablename')

例子:

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

只是我的代码

SQL Server

sp_help tablename

SQL Server

SELECT 
    c.name 
FROM
    sys.objects o
INNER JOIN
    sys.columns c
ON
    c.object_id = o.object_id
AND o.name = 'Table_Name'

or

SELECT 
    COLUMN_NAME 
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME  = 'Table_Name'

第二种方法是ANSI标准,因此应该适用于所有符合ANSI标准的数据库。